- Timestamp:
- 05/29/09 16:44:27 (16 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableAttribute.cs
r1938 r1960 1 1 using System; 2 using System.Linq; 2 3 using System.Collections.Generic; 3 4 using System.Reflection; … … 42 43 public StorableAttribute Attribute { get; private set; } 43 44 public MemberInfo MemberInfo { get; private set; } 45 public string DisentangledName { get; private set; } 46 public string FullyQualifiedMemberName { 47 get { 48 return new StringBuilder() 49 .Append(MemberInfo.ReflectedType.FullName) 50 .Append('.') 51 .Append(MemberInfo.Name) 52 .ToString(); 53 } 54 } 44 55 public StorableMemberInfo(StorableAttribute attribute, MemberInfo memberInfo) { 45 56 this.Attribute = attribute; … … 50 61 .Append('[').Append(Attribute).Append(", ") 51 62 .Append(MemberInfo).Append('}').ToString(); 63 } 64 public void SetDisentangledName(string name) { 65 DisentangledName = Attribute.Name ?? name; 66 } 67 public Type GetPropertyDeclaringBaseType() { 68 return ((PropertyInfo)MemberInfo).GetGetMethod(true).GetBaseDefinition().DeclaringType; 52 69 } 53 70 } … … 91 108 } 92 109 } 93 return storableMembers;110 return DisentangleNameMapping(storableMembers); 94 111 } 95 112 96 public static Dictionary<string, DataMemberAccessor> GetStorableAccessors(object obj) { 97 var storableAccessors = new Dictionary<string, DataMemberAccessor>(); 98 var nameMapping = createNameMapping(obj.GetType()); 99 var finalNameMapping = analyzeNameMapping(nameMapping); 100 foreach (var mapping in finalNameMapping) { 101 storableAccessors.Add(mapping.Value.Attribute.Name ?? mapping.Key, 102 new DataMemberAccessor( 103 mapping.Value.MemberInfo, 104 mapping.Value.Attribute.Name ?? mapping.Key, 105 mapping.Value.Attribute.DefaultValue, 106 obj)); 107 } 108 return storableAccessors; 113 public static IEnumerable<DataMemberAccessor> GetStorableAccessors(object obj) { 114 foreach (var memberInfo in GetStorableMembers(obj.GetType())) 115 yield return new DataMemberAccessor( 116 memberInfo.MemberInfo, 117 memberInfo.DisentangledName, 118 memberInfo.Attribute.DefaultValue, 119 obj); 109 120 } 110 121 111 private static Dictionary<string, StorableMemberInfo> analyzeNameMapping( 112 Dictionary<string, List<StorableMemberInfo>> nameMapping) { 113 var finalNameMapping = new Dictionary<string, StorableMemberInfo>(); 114 foreach (var attributes in nameMapping) { 115 if (attributes.Value.Count == 1) { 116 finalNameMapping[attributes.Key] = attributes.Value[0]; 117 } else if (attributes.Value[0].MemberInfo.MemberType == MemberTypes.Field) { 118 foreach (var attribute in attributes.Value) { 119 StringBuilder sb = new StringBuilder(); 120 sb.Append(attribute.MemberInfo.ReflectedType.FullName).Append('.') 121 .Append(attribute.MemberInfo.Name); 122 finalNameMapping[sb.ToString()] = attribute; 122 private static IEnumerable<StorableMemberInfo> DisentangleNameMapping( 123 IEnumerable<StorableMemberInfo> storableMemberInfos) { 124 var nameGrouping = new Dictionary<string, List<StorableMemberInfo>>(); 125 foreach (StorableMemberInfo storable in storableMemberInfos) { 126 if (!nameGrouping.ContainsKey(storable.MemberInfo.Name)) 127 nameGrouping[storable.MemberInfo.Name] = new List<StorableMemberInfo>(); 128 nameGrouping[storable.MemberInfo.Name].Add(storable); 129 } 130 var memberInfos = new List<StorableMemberInfo>(); 131 foreach (var storableMemberInfoGroup in nameGrouping.Values) { 132 if (storableMemberInfoGroup.Count == 1) { 133 storableMemberInfoGroup[0].SetDisentangledName(storableMemberInfoGroup[0].MemberInfo.Name); 134 memberInfos.Add(storableMemberInfoGroup[0]); 135 } else if (storableMemberInfoGroup[0].MemberInfo.MemberType == MemberTypes.Field) { 136 foreach (var storableMemberInfo in storableMemberInfoGroup) { 137 storableMemberInfo.SetDisentangledName(storableMemberInfo.FullyQualifiedMemberName); 138 memberInfos.Add(storableMemberInfo); 123 139 } 124 } else { 125 var uniqueAccessors = new Dictionary<Type, StorableMemberInfo>(); 126 foreach (var attribute in attributes.Value) { 127 uniqueAccessors[((PropertyInfo)attribute.MemberInfo).GetGetMethod(true).GetBaseDefinition().DeclaringType] = 128 attribute; 129 } 130 if (uniqueAccessors.Count == 1) { 131 var it = uniqueAccessors.Values.GetEnumerator(); 132 it.MoveNext(); 133 finalNameMapping[attributes.Key] = it.Current; 134 } else { 135 foreach (var attribute in uniqueAccessors.Values) { 136 StringBuilder sb = new StringBuilder(); 137 sb.Append(attribute.MemberInfo.DeclaringType.FullName).Append('.') 138 .Append(attribute.MemberInfo.Name); 139 finalNameMapping[sb.ToString()] = attribute; 140 } 141 } 140 } else { 141 memberInfos.AddRange(MergePropertyAccessors(storableMemberInfoGroup)); 142 142 } 143 143 } 144 return finalNameMapping;144 return memberInfos; 145 145 } 146 147 private static Dictionary<string, List<StorableMemberInfo>> createNameMapping(Type type) { 148 var nameMapping = new Dictionary<string, List<StorableMemberInfo>>(); 149 foreach (StorableMemberInfo storable in GetStorableMembers(type)) { 150 if (!nameMapping.ContainsKey(storable.MemberInfo.Name)) 151 nameMapping[storable.MemberInfo.Name] = new List<StorableMemberInfo>(); 152 nameMapping[storable.MemberInfo.Name].Add(storable); 146 147 private static IEnumerable<StorableMemberInfo> MergePropertyAccessors(List<StorableMemberInfo> members) { 148 var uniqueAccessors = new Dictionary<Type, StorableMemberInfo>(); 149 foreach (var member in members) 150 uniqueAccessors[member.GetPropertyDeclaringBaseType()] = member; 151 if (uniqueAccessors.Count == 1) { 152 var storableMemberInfo = uniqueAccessors.Values.First(); 153 storableMemberInfo.SetDisentangledName(storableMemberInfo.MemberInfo.Name); 154 yield return storableMemberInfo; 155 } else { 156 foreach (var attribute in uniqueAccessors.Values) { 157 attribute.SetDisentangledName(attribute.FullyQualifiedMemberName); 158 yield return attribute; 159 } 153 160 } 154 return nameMapping; 155 } 161 } 156 162 } 157 163 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs
r1938 r1960 33 33 34 34 public IEnumerable<Tag> Decompose(object obj) { 35 foreach (var mappingin StorableAttribute.GetStorableAccessors(obj)) {36 yield return new Tag( mapping.Key, mapping.Value.Get());35 foreach (var accessor in StorableAttribute.GetStorableAccessors(obj)) { 36 yield return new Tag(accessor.Name, accessor.Get()); 37 37 } 38 38 } … … 48 48 memberDict.Add(iter.Current.Name, iter.Current); 49 49 } 50 foreach (var mapping in StorableAttribute.GetStorableAccessors(instance)) { 51 string name = mapping.Key; 52 if (memberDict.ContainsKey(name)) { 53 mapping.Value.Set(memberDict[name].Value); 54 } else if (mapping.Value.DefaultValue != null) { 55 mapping.Value.Set(mapping.Value.DefaultValue); 50 foreach (var accessor in StorableAttribute.GetStorableAccessors(instance)) { 51 if (memberDict.ContainsKey(accessor.Name)) { 52 accessor.Set(memberDict[accessor.Name].Value); 53 } else if (accessor.DefaultValue != null) { 54 accessor.Set(accessor.DefaultValue); 56 55 } 57 56 } -
trunk/sources/HeuristicLab.Persistence/UnitTests/StorableAttributeTests.cs
r1938 r1960 56 56 public void SimpleStorableAttributeTest() { 57 57 DemoClass t = new DemoClass(); 58 Dictionary<string, DataMemberAccessor> accessors = StorableAttribute.GetStorableAccessors(t); 58 IEnumerable<DataMemberAccessor> accessorList = StorableAttribute.GetStorableAccessors(t); 59 Dictionary<string, DataMemberAccessor> accessors = new Dictionary<string, DataMemberAccessor>(); 60 foreach (var a in accessorList) 61 accessors.Add(a.Name, a); 59 62 Assert.IsTrue(accessors.ContainsKey("TestProperty")); 60 63 Assert.IsTrue(accessors.ContainsKey("x")); … … 78 81 var accessDict = new Dictionary<string, DataMemberAccessor>(); 79 82 foreach (var accessor in accessors) // assert uniqueness 80 accessDict.Add(accessor. Key, accessor.Value);83 accessDict.Add(accessor.Name, accessor); 81 84 Assert.IsTrue(accessDict.ContainsKey(typeof(New).FullName + ".Name")); 82 85 Assert.IsTrue(accessDict.ContainsKey(typeof(Override).FullName + ".Name"));
Note: See TracChangeset
for help on using the changeset viewer.