Mercurial > pub > bltoolkit
comparison Source/Reflection/Extension/ValueCollection.cs @ 0:f990fcb411a9
Копия текущей версии из github
| author | cin |
|---|---|
| date | Thu, 27 Mar 2014 21:46:09 +0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f990fcb411a9 |
|---|---|
| 1 using System; | |
| 2 | |
| 3 namespace BLToolkit.Reflection.Extension | |
| 4 { | |
| 5 public class ValueCollection : System.Collections.Generic.Dictionary<string,object> | |
| 6 { | |
| 7 private object _value; | |
| 8 public object Value | |
| 9 { | |
| 10 get { return _value; } | |
| 11 } | |
| 12 | |
| 13 public new object this[string name] | |
| 14 { | |
| 15 get | |
| 16 { | |
| 17 object value; | |
| 18 return TryGetValue(name, out value) ? value : null; | |
| 19 } | |
| 20 } | |
| 21 | |
| 22 public void Add(string name, string value) | |
| 23 { | |
| 24 if (this != _null) | |
| 25 { | |
| 26 var isType = name.EndsWith(TypeExtension.ValueName.TypePostfix); | |
| 27 | |
| 28 if (isType) | |
| 29 { | |
| 30 var type = Type.GetType(value, true); | |
| 31 var valueName = name.Substring(0, name.Length - TypeExtension.ValueName.TypePostfix.Length); | |
| 32 | |
| 33 Add(name, type); | |
| 34 | |
| 35 object val; | |
| 36 | |
| 37 TryGetValue(valueName, out val); | |
| 38 | |
| 39 if (val != null && val.GetType() != type) | |
| 40 { | |
| 41 base[valueName] = val = TypeExtension.ChangeType(val.ToString(), type); | |
| 42 | |
| 43 if (valueName == TypeExtension.ValueName.Value) | |
| 44 _value = val; | |
| 45 } | |
| 46 } | |
| 47 else | |
| 48 { | |
| 49 object val; | |
| 50 | |
| 51 var type = TryGetValue(name + TypeExtension.ValueName.TypePostfix, out val) ? (Type)val : null; | |
| 52 | |
| 53 val = value; | |
| 54 | |
| 55 if (type != null && type != _value.GetType()) | |
| 56 val = TypeExtension.ChangeType(value, type); | |
| 57 | |
| 58 if (ContainsKey(name)) | |
| 59 base[name] = val; | |
| 60 else | |
| 61 Add(name, val); | |
| 62 | |
| 63 if (name == TypeExtension.ValueName.Value) | |
| 64 _value = val; | |
| 65 } | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 private static readonly Extension.ValueCollection _null = new Extension.ValueCollection(); | |
| 70 public static Extension.ValueCollection Null | |
| 71 { | |
| 72 get { return _null; } | |
| 73 } | |
| 74 } | |
| 75 } |
