Mercurial > pub > bltoolkit
comparison Source/Reflection/Extension/AttributeNameCollection.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 using System.Collections.Generic; | |
| 3 | |
| 4 namespace BLToolkit.Reflection.Extension | |
| 5 { | |
| 6 public class AttributeNameCollection : Dictionary<string,AttributeExtensionCollection> | |
| 7 { | |
| 8 public new AttributeExtensionCollection this[string attributeName] | |
| 9 { | |
| 10 get | |
| 11 { | |
| 12 if (this == _null) | |
| 13 return AttributeExtensionCollection.Null; | |
| 14 | |
| 15 AttributeExtensionCollection ext; | |
| 16 | |
| 17 return TryGetValue(attributeName, out ext) ? ext : AttributeExtensionCollection.Null; | |
| 18 } | |
| 19 } | |
| 20 | |
| 21 public void Add(AttributeExtension attributeExtension) | |
| 22 { | |
| 23 if (this != _null) | |
| 24 { | |
| 25 // Add attribute. | |
| 26 // | |
| 27 AttributeExtensionCollection attr; | |
| 28 | |
| 29 if (!TryGetValue(attributeExtension.Name, out attr)) | |
| 30 Add(attributeExtension.Name, attr = new AttributeExtensionCollection()); | |
| 31 | |
| 32 attr.Add(attributeExtension); | |
| 33 | |
| 34 /* | |
| 35 // Convert value type. | |
| 36 // | |
| 37 bool isType = attributeExtension.Name.EndsWith(TypeExtension.AttrName.TypePostfix); | |
| 38 | |
| 39 if (isType) | |
| 40 { | |
| 41 string attrName = attributeExtension.Name.Substring( | |
| 42 0, attributeExtension.Name.Length - 5); | |
| 43 | |
| 44 AttributeExtensionCollection ext = | |
| 45 (AttributeExtensionCollection)_attributes[attrName]; | |
| 46 | |
| 47 if (ext != null && ext.Count == 1) | |
| 48 ext[0].Values.ChangeValueType(attributeExtension.Value.ToString()); | |
| 49 } | |
| 50 else | |
| 51 { | |
| 52 string attrName = attributeExtension.Name + TypeExtension.AttrName.TypePostfix; | |
| 53 | |
| 54 AttributeExtensionCollection ext = | |
| 55 (AttributeExtensionCollection)_attributes[attrName]; | |
| 56 | |
| 57 if (ext != null && ext.Count == 1) | |
| 58 attributeExtension.Values.ChangeValueType(ext.Value.ToString()); | |
| 59 } | |
| 60 */ | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 public void Add(string name, string value) | |
| 65 { | |
| 66 if (this != _null) | |
| 67 { | |
| 68 var attrName = name; | |
| 69 var valueName = string.Empty; | |
| 70 var idx = name.IndexOf(TypeExtension.ValueName.Delimiter); | |
| 71 | |
| 72 if (idx > 0) | |
| 73 { | |
| 74 valueName = name.Substring(idx + 1).TrimStart(TypeExtension.ValueName.Delimiter); | |
| 75 attrName = name.Substring(0, idx); | |
| 76 } | |
| 77 | |
| 78 if (valueName.Length == 0) | |
| 79 valueName = TypeExtension.ValueName.Value; | |
| 80 else if (valueName == TypeExtension.ValueName.Type) | |
| 81 valueName = TypeExtension.ValueName.ValueType; | |
| 82 | |
| 83 AttributeExtensionCollection ext; | |
| 84 | |
| 85 if (TryGetValue(attrName, out ext)) | |
| 86 ext[0].Values.Add(valueName, value); | |
| 87 else | |
| 88 { | |
| 89 var attributeExtension = new AttributeExtension { Name = name }; | |
| 90 | |
| 91 attributeExtension.Values.Add(valueName, value); | |
| 92 | |
| 93 Add(attributeExtension); | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 private static readonly AttributeNameCollection _null = new AttributeNameCollection(); | |
| 99 public static AttributeNameCollection Null | |
| 100 { | |
| 101 get { return _null; } | |
| 102 } | |
| 103 } | |
| 104 } |
