Changeset 11143
- Timestamp:
- 07/08/14 18:58:15 (10 years ago)
- Location:
- stable
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
stable
- Property svn:mergeinfo changed
/trunk/sources merged: 10958
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Persistence
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Persistence merged: 10958
- Property svn:mergeinfo changed
-
stable/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs
r9456 r11143 47 47 hookCache = new Dictionary<HookDesignator, List<StorableReflection.Hook>>(); 48 48 } 49 49 50 [StorableConstructor] 50 51 private StorableSerializer(bool deserializing) : this() { } … … 68 69 /// </returns> 69 70 public bool CanSerialize(Type type) { 70 boolmarkedStorable = StorableReflection.HasStorableClassAttribute(type);71 var markedStorable = StorableReflection.HasStorableClassAttribute(type); 71 72 if (GetConstructor(type) == null) 72 73 if (markedStorable) … … 91 92 /// </returns> 92 93 public string JustifyRejection(Type type) { 93 StringBuilder sb = new StringBuilder();94 var sb = new StringBuilder(); 94 95 if (GetConstructor(type) == null) 95 96 sb.Append("class has no default constructor and no [StorableConstructor]"); … … 117 118 /// <returns>An enumerable of <see cref="Tag"/>s.</returns> 118 119 public IEnumerable<Tag> Decompose(object obj) { 119 foreach (var accessor in GetStorableAccessors(obj.GetType())) { 120 if (accessor.Get != null) 121 yield return new Tag(accessor.Name, accessor.Get(obj)); 122 } 120 return from accessor in GetStorableAccessors(obj.GetType()) 121 where accessor.Get != null 122 select new Tag(accessor.Name, accessor.Get(obj)); 123 123 } 124 124 … … 147 147 public void Populate(object instance, IEnumerable<Tag> objects, Type type) { 148 148 var memberDict = new Dictionary<string, Tag>(); 149 IEnumerator<Tag>iter = objects.GetEnumerator();149 var iter = objects.GetEnumerator(); 150 150 while (iter.MoveNext()) { 151 151 memberDict.Add(iter.Current.Name, iter.Current); … … 170 170 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; 171 171 172 private static readonly object[] emptyArgs = new object[] { }; 173 private static readonly object[] trueArgs = new object[] { true }; 174 175 private sealed class HookDesignator { 176 public Type Type { get; private set; } 177 public HookType HookType { get; private set; } 178 public HookDesignator() { } 179 public HookDesignator(Type type, HookType hookType) { 180 Type = type; 181 HookType = HookType; 182 } 172 private sealed class HookDesignator : Tuple<Type, HookType> { 173 public HookDesignator(Type type, HookType hookType) : base(type, hookType) { } 183 174 } 184 175 … … 191 182 #region caches 192 183 193 private AccessorListCache accessorListCache;194 private AccessorCache accessorCache;195 private Dictionary<Type, Constructor> constructorCache;196 private Dictionary<HookDesignator, List<StorableReflection.Hook>> hookCache;184 private readonly AccessorListCache accessorListCache; 185 private readonly AccessorCache accessorCache; 186 private readonly Dictionary<Type, Constructor> constructorCache; 187 private readonly Dictionary<HookDesignator, List<StorableReflection.Hook>> hookCache; 197 188 198 189 #endregion … … 206 197 var storableMembers = StorableReflection 207 198 .GenerateStorableMembers(type) 208 .Select(mi => GetMemberAccessor(mi)); 199 .Select(GetMemberAccessor) 200 .ToList(); 209 201 accessorListCache[type] = storableMembers; 210 202 return storableMembers; … … 216 208 if (accessorCache.ContainsKey(mi.MemberInfo)) 217 209 return new DataMemberAccessor(accessorCache[mi.MemberInfo], mi.DisentangledName, mi.DefaultValue); 218 DataMemberAccessor dma = new DataMemberAccessor(mi.MemberInfo, mi.DisentangledName, mi.DefaultValue);210 var dma = new DataMemberAccessor(mi.MemberInfo, mi.DisentangledName, mi.DefaultValue); 219 211 accessorCache[mi.MemberInfo] = dma; 220 212 return dma; … … 226 218 if (constructorCache.ContainsKey(type)) 227 219 return constructorCache[type]; 228 Constructor c = FindStorableConstructor(type) ?? GetDefaultConstructor(type);220 var c = FindStorableConstructor(type) ?? GetDefaultConstructor(type); 229 221 constructorCache.Add(type, c); 230 222 return c; … … 233 225 234 226 private Constructor GetDefaultConstructor(Type type) { 235 ConstructorInfoci = type.GetConstructor(ALL_CONSTRUCTORS, null, Type.EmptyTypes, null);227 var ci = type.GetConstructor(ALL_CONSTRUCTORS, null, Type.EmptyTypes, null); 236 228 if (ci == null) 237 229 return null; 238 DynamicMethoddm = new DynamicMethod("", typeof(object), null, type, true);239 ILGenerator ilgen = dm.GetILGenerator();230 var dm = new DynamicMethod("", typeof(object), null, type, true); 231 var ilgen = dm.GetILGenerator(); 240 232 ilgen.Emit(OpCodes.Newobj, ci); 241 233 ilgen.Emit(OpCodes.Ret); … … 244 236 245 237 private Constructor FindStorableConstructor(Type type) { 246 foreach ( ConstructorInfo ci in type.GetConstructors(ALL_CONSTRUCTORS)) {247 if (ci.GetCustomAttributes(typeof(StorableConstructorAttribute), false).Length > 0) {248 if (ci.GetParameters().Length != 1 ||249 ci.GetParameters()[0].ParameterType != typeof(bool))250 throw new PersistenceException("StorableConstructor must have exactly one argument of type bool");251 DynamicMethod dm = new DynamicMethod("", typeof(object), null, type, true);252 ILGenerator ilgen = dm.GetILGenerator();253 ilgen.Emit(OpCodes.Ldc_I4_1); // load true254 ilgen.Emit(OpCodes.Newobj, ci);255 ilgen.Emit(OpCodes.Ret);256 return (Constructor)dm.CreateDelegate(typeof(Constructor));257 }238 foreach (var ci in type 239 .GetConstructors(ALL_CONSTRUCTORS) 240 .Where(ci => ci.GetCustomAttributes(typeof(StorableConstructorAttribute), false).Length > 0)) { 241 if (ci.GetParameters().Length != 1 || 242 ci.GetParameters()[0].ParameterType != typeof(bool)) 243 throw new PersistenceException("StorableConstructor must have exactly one argument of type bool"); 244 var dm = new DynamicMethod("", typeof(object), null, type, true); 245 var ilgen = dm.GetILGenerator(); 246 ilgen.Emit(OpCodes.Ldc_I4_1); // load true 247 ilgen.Emit(OpCodes.Newobj, ci); 248 ilgen.Emit(OpCodes.Ret); 249 return (Constructor)dm.CreateDelegate(typeof(Constructor)); 258 250 } 259 251 return null; … … 262 254 private void InvokeHook(HookType hookType, object obj) { 263 255 if (obj == null) 264 throw new ArgumentNullException(" Cannot invoke hooks on null");265 foreach ( StorableReflection.Hookhook in GetHooks(hookType, obj.GetType())) {256 throw new ArgumentNullException("obj"); 257 foreach (var hook in GetHooks(hookType, obj.GetType())) { 266 258 hook(obj); 267 259 }
Note: See TracChangeset
for help on using the changeset viewer.