/
DynamicDictionary.cs
313 lines (277 loc) · 9.71 KB
/
DynamicDictionary.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Owin.HelloWorld
{
/// <summary>
/// An implementation of a <see cref="System.Collections.Generic.IDictionary<string, TValue>"/> which uses dynamics to allow property accessors
/// </summary>
/// <remarks>
/// <para>
/// This class inherits from <see cref="System.Dynamic.DynamicObject"/> and <see cref="System.Collections.Generic.IDictionary<string, TValue>"/> (to give default dictionary features as well).
/// It allows you to have a dictionary which you access the key store via standard dot-notation. This is exposed via extension methods for users to create.
/// </para>
/// <example>
/// var dictionary = new Dictionary<string, string> { { "hello", "world" } };
/// var dynamicDictionary = dictionary.AsDynamic();
/// //access data
/// var local = dynamicDictionary.hello;
/// //create new key
/// dynamicDictionary.newValue = "I'm new!";
/// </example>
/// </remarks>
/// <typeparam name="string">The type of the key.</typeparam>
/// <typeparam name="TValue">The type of the value.</typeparam>
internal sealed class DynamicDictionary<TValue> : DynamicObject, IDictionary<string, TValue>
{
private IDictionary<string, TValue> dictionary;
/// <summary>
/// Initializes a new instance of the <see cref="DynamicDictionary<string, TValue>"/> class.
/// </summary>
public DynamicDictionary()
: this(new Dictionary<string, TValue>())
{
}
/// <summary>
/// Initializes a new instance of the <see cref="DynamicDictionary<string, TValue>"/> class from a given dictionary
/// </summary>
/// <param name="dictionary">The dictionary.</param>
public DynamicDictionary(IDictionary<string, TValue> dictionary)
{
//set the internal dictionary instance
this.dictionary = dictionary;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
var key = binder.Name;
//check if the key exists in the dictionary
if (dictionary.ContainsKey(key))
{
//set it and return true to indicate its found
result = dictionary[key];
return true;
}
//look into the base implementation, it might be there
var found = base.TryGetMember(binder, out result);
//if it wasn't found we'll raise an exception
if (!found)
throw new KeyNotFoundException(string.Format("Key \"{0}\" was not found in the given dictionary", key));
return found;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
if (this.IsReadOnly)
throw new InvalidOperationException("This dictionary instance is read-only, you cannot modify the data it contains");
var key = binder.Name;
//check if the dictionary already has this key
if (dictionary.ContainsKey(key))
{
//it did so we can assign it to the new value
dictionary[key] = (TValue)value;
return true;
}
else
{
//check the base for the property
var found = base.TrySetMember(binder, value);
//if it wasn't found then the user must have wanted a new key
//we'll expect implicit casting here, and an exception will be raised
//if it cannot explicitly cast
if (!found)
dictionary.Add(key, (TValue)value);
return true;
}
}
public override bool TryDeleteMember(DeleteMemberBinder binder)
{
var key = binder.Name;
if (dictionary.ContainsKey(key))
{
dictionary.Remove(key);
return true;
}
else
{
//throw new KeyNotFoundException(string.Format("Key \"{0}\" was not found in the given dictionary", key));
return false;
}
}
//public void Add(DynamicKeyValuePair<string, TValue> item)
//{
// this.dictionary.Add(new KeyValuePair<string, TValue>(item.Key, item.Value));
//}
#region IDictionary<string,TValue> Members
/// <summary>
/// Adds the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void Add(string key, TValue value)
{
this.dictionary.Add(key, value);
}
/// <summary>
/// Determines whether the specified key contains key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns>
/// <c>true</c> if the specified key contains key; otherwise, <c>false</c>.
/// </returns>
public bool ContainsKey(string key)
{
return this.dictionary.ContainsKey(key);
}
/// <summary>
/// Gets the keys.
/// </summary>
/// <value>The keys.</value>
public ICollection<string> Keys
{
get
{
return this.dictionary.Keys;
}
}
/// <summary>
/// Removes the specified key.
/// </summary>
/// <param name="key">The key.</param>
/// <returns></returns>
public bool Remove(string key)
{
return this.dictionary.Remove(key);
}
/// <summary>
/// Tries the get value.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public bool TryGetValue(string key, out TValue value)
{
return this.dictionary.TryGetValue(key, out value);
}
/// <summary>
/// Gets the values.
/// </summary>
/// <value>The values.</value>
public ICollection<TValue> Values
{
get
{
return this.dictionary.Values;
}
}
/// <summary>
/// Gets or sets the <see cref="TValue"/> with the specified key.
/// </summary>
/// <value></value>
public TValue this[string key]
{
get
{
return this.dictionary[key];
}
set
{
this.dictionary[key] = value;
}
}
#endregion
#region ICollection<KeyValuePair<string,TValue>> Members
/// <summary>
/// Adds the specified item.
/// </summary>
/// <param name="item">The item.</param>
public void Add(KeyValuePair<string, TValue> item)
{
this.dictionary.Add(item);
}
/// <summary>
/// Clears this instance.
/// </summary>
public void Clear()
{
this.dictionary.Clear();
}
/// <summary>
/// Determines whether [contains] [the specified item].
/// </summary>
/// <param name="item">The item.</param>
/// <returns>
/// <c>true</c> if [contains] [the specified item]; otherwise, <c>false</c>.
/// </returns>
public bool Contains(KeyValuePair<string, TValue> item)
{
return this.dictionary.Contains(item);
}
/// <summary>
/// Copies to.
/// </summary>
/// <param name="array">The array.</param>
/// <param name="arrayIndex">Index of the array.</param>
public void CopyTo(KeyValuePair<string, TValue>[] array, int arrayIndex)
{
this.dictionary.CopyTo(array, arrayIndex);
}
/// <summary>
/// Gets the count.
/// </summary>
/// <value>The count.</value>
public int Count
{
get
{
return this.dictionary.Count;
}
}
/// <summary>
/// Gets a value indicating whether this instance is read only.
/// </summary>
/// <value>
/// <c>true</c> if this instance is read only; otherwise, <c>false</c>.
/// </value>
public bool IsReadOnly
{
get
{
return this.dictionary.IsReadOnly;
}
}
/// <summary>
/// Removes the specified item.
/// </summary>
/// <param name="item">The item.</param>
/// <returns></returns>
public bool Remove(KeyValuePair<string, TValue> item)
{
return this.dictionary.Remove(item);
}
#endregion
#region IEnumerable<KeyValuePair<string,TValue>> Members
/// <summary>
/// Gets the enumerator.
/// </summary>
/// <returns></returns>
public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator()
{
return this.dictionary.GetEnumerator();
}
#endregion
#region IEnumerable Members
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
}