|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Dynamic; |
| 4 | +using System.Linq; |
| 5 | +using System.Reflection; |
| 6 | +using System.Text; |
| 7 | + |
| 8 | +namespace System.Linq.Dynamic |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// Provides a base class for dynamic objects created by using the <see cref="DynamicQueryable.Select(IQueryable,string,object[])"/> |
| 12 | + /// method. For internal use only. |
| 13 | + /// </summary> |
| 14 | + public class DynamicClass : DynamicObject |
| 15 | + { |
| 16 | + Dictionary<string, object> _properties = new Dictionary<string, object>(); |
| 17 | + |
| 18 | + //public DynamicClass(IEnumerable<KeyValuePair<string, object>> propertyValues) |
| 19 | + //{ |
| 20 | + // foreach (var pair in propertyValues) |
| 21 | + // { |
| 22 | + // _properties.Add(pair.Key, pair.Value); |
| 23 | + // } |
| 24 | + //} |
| 25 | + |
| 26 | + public DynamicClass(KeyValuePair<string, object> _1) |
| 27 | + { |
| 28 | + _properties.Add(_1.Key, _1.Value); |
| 29 | + } |
| 30 | + |
| 31 | + public DynamicClass(KeyValuePair<string, object> _1, KeyValuePair<string, object> _2) |
| 32 | + { |
| 33 | + _properties.Add(_1.Key, _1.Value); |
| 34 | + _properties.Add(_2.Key, _2.Value); |
| 35 | + } |
| 36 | + |
| 37 | + public DynamicClass(KeyValuePair<string, object> _1, KeyValuePair<string, object> _2, KeyValuePair<string, object> _3) |
| 38 | + { |
| 39 | + _properties.Add(_1.Key, _1.Value); |
| 40 | + _properties.Add(_2.Key, _2.Value); |
| 41 | + _properties.Add(_3.Key, _3.Value); |
| 42 | + } |
| 43 | + |
| 44 | + public DynamicClass(KeyValuePair<string, object> _1, KeyValuePair<string, object> _2, KeyValuePair<string, object> _3, KeyValuePair<string, object> _4) |
| 45 | + { |
| 46 | + _properties.Add(_1.Key, _1.Value); |
| 47 | + _properties.Add(_2.Key, _2.Value); |
| 48 | + _properties.Add(_3.Key, _3.Value); |
| 49 | + _properties.Add(_4.Key, _4.Value); |
| 50 | + } |
| 51 | + |
| 52 | + public object this[string name] |
| 53 | + { |
| 54 | + get |
| 55 | + { |
| 56 | + object result; |
| 57 | + if (_properties.TryGetValue(name, out result)) |
| 58 | + return result; |
| 59 | + |
| 60 | + return null; |
| 61 | + } |
| 62 | + set |
| 63 | + { |
| 64 | + if (_properties.ContainsKey(name)) |
| 65 | + _properties[name] = value; |
| 66 | + else |
| 67 | + _properties.Add(name, value); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public override IEnumerable<string> GetDynamicMemberNames() |
| 72 | + { |
| 73 | + return _properties.Keys; |
| 74 | + } |
| 75 | + |
| 76 | + public override bool TryGetMember(GetMemberBinder binder, out object result) |
| 77 | + { |
| 78 | + var name = binder.Name; |
| 79 | + _properties.TryGetValue(name, out result); |
| 80 | + |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + public override bool TrySetMember(SetMemberBinder binder, object value) |
| 85 | + { |
| 86 | + var name = binder.Name; |
| 87 | + if (_properties.ContainsKey(name)) |
| 88 | + _properties[name] = value; |
| 89 | + else |
| 90 | + _properties.Add(name, value); |
| 91 | + |
| 92 | + return true; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments