|
1 | 1 | // Copyright (c) 2021 Samuel Abraham |
2 | 2 |
|
3 | 3 | using System.Collections; |
| 4 | +using System.Collections.Generic; |
4 | 5 | using TypeCache.Extensions; |
5 | 6 | using TypeCache.Reflection; |
6 | 7 |
|
7 | 8 | namespace TypeCache.Adapters; |
8 | 9 |
|
9 | | -internal class DictionaryAdapter : IDictionary<object, object> |
| 10 | +public class DictionaryAdapter : CollectionAdapter, IDictionary<object, object> |
10 | 11 | { |
11 | 12 | private readonly object _Dictionary; |
12 | 13 |
|
13 | | - private readonly PropertyEntity _Count; |
14 | | - private readonly PropertyEntity _Item; |
15 | | - private readonly PropertyEntity _IsReadOnly; |
| 14 | + private readonly PropertyIndexerEntity _Item; |
16 | 15 | private readonly PropertyEntity _Keys; |
17 | 16 | private readonly PropertyEntity _Values; |
18 | | - private readonly MethodSet<MethodEntity> _Add; |
19 | | - private readonly MethodSet<MethodEntity> _Clear; |
20 | | - private readonly MethodSet<MethodEntity> _ContainsKey; |
21 | | - private readonly MethodSet<MethodEntity> _CopyTo; |
22 | | - private readonly MethodSet<MethodEntity> _GetEnumerator; |
23 | | - private readonly MethodSet<MethodEntity> _Remove; |
| 17 | + private readonly MethodEntity _Add; |
| 18 | + private readonly MethodEntity _ContainsKey; |
| 19 | + private readonly MethodEntity _Remove; |
24 | 20 |
|
25 | 21 | public DictionaryAdapter(object dictionary) |
| 22 | + : base(dictionary) |
26 | 23 | { |
27 | 24 | this._Dictionary = dictionary; |
| 25 | + (dictionary.GetType().Implements(typeof(IDictionary<,>))).ThrowIfFalse(); |
28 | 26 |
|
29 | | - var methods = dictionary.GetType().Methods(); |
30 | | - var properties = dictionary.GetType().Properties(); |
31 | | - |
32 | | - this._Count = properties[nameof(Count)]; |
33 | | - this._Item = properties["Item"]; |
34 | | - this._IsReadOnly = properties[nameof(IsReadOnly)]; |
35 | | - this._Keys = properties[nameof(Keys)]; |
36 | | - this._Values = properties[nameof(Values)]; |
37 | | - this._Add = methods[nameof(Add)]; |
38 | | - this._Clear = methods[nameof(Clear)]; |
39 | | - this._ContainsKey = methods[nameof(ContainsKey)]; |
40 | | - this._CopyTo = methods[nameof(CopyTo)]; |
41 | | - this._GetEnumerator = methods[nameof(GetEnumerator)]; |
42 | | - this._Remove = methods[nameof(Remove)]; |
| 27 | + var dictionaryType = dictionary.GetType().GetInterfaces().First(_ => _.Is(typeof(IDictionary<,>))); |
| 28 | + |
| 29 | + this._Item = dictionaryType.DefaultIndexer()!; |
| 30 | + this._Keys = dictionaryType.Properties()[nameof(Keys)]; |
| 31 | + this._Values = dictionaryType.Properties()[nameof(Values)]; |
| 32 | + this._Add = dictionaryType.Methods()[nameof(Add)].First(_ => _.Parameters.Count is 2); |
| 33 | + this._ContainsKey = dictionaryType.Methods()[nameof(ContainsKey)].First(); |
| 34 | + this._Remove = dictionaryType.Methods()[nameof(Remove)].First(_ => _.Parameters[0].ParameterType == dictionaryType.GetGenericArguments()[0]); |
43 | 35 | } |
44 | 36 |
|
45 | 37 | public object this[object key] |
46 | 38 | { |
47 | | - get => this._Item[ValueTuple.Create(key)].GetValue(this._Dictionary)!; |
48 | | - set => this._Item[ValueTuple.Create(key)].SetValue(this._Dictionary, value); |
| 39 | + get => this._Item.GetValue(this._Dictionary, ValueTuple.Create(key))!; |
| 40 | + set => this._Item.SetValue(this._Dictionary, ValueTuple.Create(key), value); |
49 | 41 | } |
50 | 42 |
|
51 | 43 | public ICollection<object> Keys => new CollectionAdapter(this._Keys.GetValue(this._Dictionary)!); |
52 | 44 |
|
53 | 45 | public ICollection<object> Values => new CollectionAdapter(this._Values.GetValue(this._Dictionary)!); |
54 | 46 |
|
55 | | - public int Count => (int)_Count.GetValue(this._Dictionary)!; |
56 | | - |
57 | | - public bool IsReadOnly => (bool)_IsReadOnly.GetValue(this._Dictionary)!; |
58 | | - |
59 | 47 | public void Add(object key, object value) |
60 | | - => _ = this._Add.Find([key, value])!.Invoke(this._Dictionary, [key, value]); |
| 48 | + => _ = this._Add.Invoke(this._Dictionary, [key, value]); |
61 | 49 |
|
62 | 50 | public void Add(KeyValuePair<object, object> item) |
63 | 51 | => throw new NotImplementedException(); |
64 | 52 |
|
65 | | - public void Clear() |
66 | | - => _ = this._Clear.FindWithNoArguments()!.Invoke(this._Dictionary); |
67 | | - |
68 | 53 | public bool Contains(KeyValuePair<object, object> item) |
69 | 54 | => throw new NotImplementedException(); |
70 | 55 |
|
71 | 56 | public bool ContainsKey(object key) |
72 | | - => (bool)this._ContainsKey.Find(key.ToValueTuple())!.Invoke(this._Dictionary, [key])!; |
| 57 | + => (bool)this._ContainsKey.Invoke(this._Dictionary, [key])!; |
73 | 58 |
|
74 | 59 | public void CopyTo(KeyValuePair<object, object>[] array, int arrayIndex) |
75 | 60 | => throw new NotImplementedException(); |
76 | 61 |
|
77 | | - public IEnumerator<KeyValuePair<object, object>> GetEnumerator() |
| 62 | + IEnumerator<KeyValuePair<object, object>> IEnumerable<KeyValuePair<object, object>>.GetEnumerator() |
78 | 63 | => throw new NotImplementedException(); |
79 | 64 |
|
80 | 65 | public bool Remove(object key) |
81 | | - => (bool)this._Remove.Find(key.ToValueTuple())!.Invoke(this._Dictionary, [key])!; |
| 66 | + => (bool)this._Remove.Invoke(this._Dictionary, [key])!; |
82 | 67 |
|
83 | 68 | public bool Remove(KeyValuePair<object, object> item) |
84 | 69 | => throw new NotImplementedException(); |
85 | 70 |
|
86 | 71 | public bool TryGetValue(object key, [MaybeNullWhen(false)] out object value) |
87 | 72 | => throw new NotImplementedException(); |
88 | | - |
89 | | - IEnumerator IEnumerable.GetEnumerator() |
90 | | - => (IEnumerator)this._GetEnumerator.FindWithNoArguments()!.Invoke(this._Dictionary)!; |
91 | 73 | } |
0 commit comments