-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExpando.cs
372 lines (327 loc) · 12 KB
/
Expando.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
using System;
using System.Linq;
using System.Collections.Generic;
using System.Dynamic;
using System.Reflection;
namespace EZms.Core
{
/// <summary>
/// Class that provides extensible properties and methods. This
/// dynamic object stores 'extra' properties in a dictionary or
/// checks the actual properties of the instance.
///
/// This means you can subclass this expando and retrieve either
/// native properties or properties from values in the dictionary.
///
/// This type allows you three ways to access its properties:
///
/// Directly: any explicitly declared properties are accessible
/// Dynamic: dynamic cast allows access to dictionary and native properties/methods
/// Dictionary: Any of the extended properties are accessible via IDictionary interface
/// </summary>
[Serializable]
public sealed class Expando : DynamicObject, IDynamicMetaObjectProvider
{
/// <summary>
/// Instance of object passed in
/// </summary>
object _instance;
/// <summary>
/// Cached type of the instance
/// </summary>
Type _instanceType;
PropertyInfo[] InstancePropertyInfo
{
get
{
if (_instancePropertyInfo == null && _instance != null)
_instancePropertyInfo = _instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);
return _instancePropertyInfo;
}
}
PropertyInfo[] _instancePropertyInfo;
/// <summary>
/// String Dictionary that contains the extra dynamic values
/// stored on this object/instance
/// </summary>
/// <remarks>Using PropertyBag to support XML Serialization of the dictionary</remarks>
public PropertyBag Properties = new PropertyBag();
//public Dictionary<string,object> Properties = new Dictionary<string, object>();
/// <inheritdoc />
/// <summary>
/// This constructor just works off the internal dictionary and any
/// public properties of this object.
/// Note you can subclass Expando.
/// </summary>
public Expando()
{
Initialize(this);
}
/// <inheritdoc />
/// <summary>
/// Allows passing in an existing instance variable to 'extend'.
/// </summary>
/// <remarks>
/// You can pass in null here if you don't want to
/// check native properties and only check the Dictionary!
/// </remarks>
/// <param name="instance"></param>
public Expando(object instance)
{
Initialize(instance);
}
private void Initialize(object instance)
{
_instance = instance;
if (instance != null)
_instanceType = instance.GetType();
}
/// <summary>
/// Try to retrieve a member by name first from instance properties
/// followed by the collection entries.
/// </summary>
/// <param name="binder"></param>
/// <param name="result"></param>
/// <returns></returns>
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
result = null;
// first check the Properties collection for member
if (Properties.Keys.Contains(binder.Name))
{
result = Properties[binder.Name];
return true;
}
// Next check for Public properties via Reflection
if (_instance != null)
{
try
{
return GetProperty(_instance, binder.Name, out result);
}
catch
{
// ignored
}
}
// failed to retrieve a property
result = null;
return false;
}
/// <summary>
/// Property setter implementation tries to retrieve value from instance
/// first then into this object
/// </summary>
/// <param name="binder"></param>
/// <param name="value"></param>
/// <returns></returns>
public override bool TrySetMember(SetMemberBinder binder, object value)
{
// first check to see if there's a native property to set
if (_instance != null)
{
try
{
bool result = SetProperty(_instance, binder.Name, value);
if (result)
return true;
}
catch
{
// ignored
}
}
// no match - set or add to dictionary
Properties[binder.Name] = value;
return true;
}
/// <summary>
/// Dynamic invocation method. Currently allows only for Reflection based
/// operation (no ability to add methods dynamically).
/// </summary>
/// <param name="binder"></param>
/// <param name="args"></param>
/// <param name="result"></param>
/// <returns></returns>
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
if (_instance != null)
{
try
{
// check instance passed in for methods to invoke
if (InvokeMethod(_instance, binder.Name, args, out result))
return true;
}
catch
{
// ignored
}
}
result = null;
return false;
}
/// <summary>
/// Reflection Helper method to retrieve a property
/// </summary>
/// <param name="instance"></param>
/// <param name="name"></param>
/// <param name="result"></param>
/// <returns></returns>
private bool GetProperty(object instance, string name, out object result)
{
if (instance == null)
instance = this;
var miArray = _instanceType.GetMember(name, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance);
if (miArray != null && miArray.Length > 0)
{
var mi = miArray[0];
if (mi.MemberType == MemberTypes.Property)
{
result = ((PropertyInfo)mi).GetValue(instance, null);
return true;
}
}
result = null;
return false;
}
/// <summary>
/// Reflection helper method to set a property value
/// </summary>
/// <param name="instance"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
private bool SetProperty(object instance, string name, object value)
{
if (instance == null)
instance = this;
var miArray = _instanceType.GetMember(name, BindingFlags.Public | BindingFlags.SetProperty | BindingFlags.Instance);
if (miArray != null && miArray.Length > 0)
{
var mi = miArray[0];
if (mi.MemberType == MemberTypes.Property)
{
((PropertyInfo)mi).SetValue(_instance, value, null);
return true;
}
}
return false;
}
/// <summary>
/// Reflection helper method to invoke a method
/// </summary>
/// <param name="instance"></param>
/// <param name="name"></param>
/// <param name="args"></param>
/// <param name="result"></param>
/// <returns></returns>
private bool InvokeMethod(object instance, string name, object[] args, out object result)
{
if (instance == null)
instance = this;
// Look at the instanceType
var miArray = _instanceType.GetMember(name,
BindingFlags.InvokeMethod |
BindingFlags.Public | BindingFlags.Instance);
if (miArray != null && miArray.Length > 0)
{
var mi = miArray[0] as MethodInfo;
result = mi.Invoke(_instance, args);
return true;
}
result = null;
return false;
}
/// <summary>
/// Convenience method that provides a string Indexer
/// to the Properties collection AND the strongly typed
/// properties of the object by name.
///
/// // dynamic
/// exp["Address"] = "112 nowhere lane";
/// // strong
/// var name = exp["StronglyTypedProperty"] as string;
/// </summary>
/// <remarks>
/// The getter checks the Properties dictionary first
/// then looks in PropertyInfo for properties.
/// The setter checks the instance properties before
/// checking the Properties dictionary.
/// </remarks>
/// <param name="key"></param>
///
/// <returns></returns>
public object this[string key]
{
get
{
try
{
// try to get from properties collection first
return Properties[key];
}
catch (KeyNotFoundException)
{
// try reflection on instanceType
object result = null;
if (GetProperty(_instance, key, out result))
return result;
// nope doesn't exist
throw;
}
}
set
{
if (Properties.ContainsKey(key))
{
Properties[key] = value;
return;
}
// check instance for existance of type first
var miArray = _instanceType.GetMember(key, BindingFlags.Public | BindingFlags.GetProperty);
if (miArray != null && miArray.Length > 0)
SetProperty(_instance, key, value);
else
Properties[key] = value;
}
}
/// <summary>
/// Returns and the properties of
/// </summary>
/// <param name="includeInstanceProperties"></param>
/// <returns></returns>
public IEnumerable<KeyValuePair<string, object>> GetProperties(bool includeInstanceProperties = false)
{
if (includeInstanceProperties && _instance != null)
{
foreach (var prop in InstancePropertyInfo)
yield return new KeyValuePair<string, object>(prop.Name, prop.GetValue(_instance, null));
}
foreach (var key in Properties.Keys)
yield return new KeyValuePair<string, object>(key, Properties[key]);
}
/// <summary>
/// Checks whether a property exists in the Property collection
/// or as a property on the instance
/// </summary>
/// <param name="item"></param>
/// <param name="includeInstanceProperties"></param>
/// <returns></returns>
public bool Contains(KeyValuePair<string, object> item, bool includeInstanceProperties = false)
{
bool res = Properties.ContainsKey(item.Key);
if (res)
return true;
if (includeInstanceProperties && _instance != null)
{
foreach (var prop in InstancePropertyInfo)
{
if (prop.Name == item.Key)
return true;
}
}
return false;
}
}
}