- Timestamp:
- 06/09/10 16:29:02 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableReflection.cs
r3742 r3913 73 73 74 74 private static object[] emptyArgs = new object[0]; 75 private static Type[] objectArg = new[] { typeof(object) }; 75 76 76 77 public static IEnumerable<Hook> CollectHooks(HookType hookType, Type type) { … … 79 80 yield return mi; 80 81 foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) { 82 if (memberInfo.MemberType != MemberTypes.Method) 83 continue; 84 MethodInfo methodInfo = memberInfo as MethodInfo; 85 if (methodInfo.ReturnType != typeof(void)) 86 continue; 87 if (methodInfo.GetParameters().Length > 0) 88 continue; 81 89 foreach (StorableHookAttribute hook in memberInfo.GetCustomAttributes(typeof(StorableHookAttribute), false)) { 82 90 if (hook != null && hook.HookType == hookType) { 83 MethodInfo methodInfo = memberInfo as MethodInfo; 84 if (memberInfo.MemberType != MemberTypes.Method || memberInfo == null) 85 throw new ArgumentException("Storable hooks must be methods"); 86 DynamicMethod dm = new DynamicMethod("", null, new[] { typeof(object) }, type); 87 ILGenerator ilgen = dm.GetILGenerator(); 88 ilgen.Emit(OpCodes.Ldarg_0); 89 ilgen.Emit(OpCodes.Callvirt, methodInfo); 90 ilgen.Emit(OpCodes.Ret); 91 yield return (Hook)dm.CreateDelegate(typeof(Hook)); 91 yield return new Hook((o) => methodInfo.Invoke(o, emptyArgs)); 92 92 } 93 93 } … … 99 99 private static void AddMarkedMembers(Type type, List<StorableMemberInfo> storableMembers) { 100 100 foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) { 101 foreach (StorableAttribute attribute in memberInfo.GetCustomAttributes(typeof(StorableAttribute), false)) { 102 storableMembers.Add(new StorableMemberInfo(attribute, memberInfo)); 101 if (memberInfo.MemberType == MemberTypes.Field || 102 memberInfo.MemberType == MemberTypes.Property) { 103 foreach (StorableAttribute attribute in memberInfo.GetCustomAttributes(typeof(StorableAttribute), false)) { 104 storableMembers.Add(new StorableMemberInfo(attribute, memberInfo)); 105 } 103 106 } 104 107 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs
r3742 r3913 109 109 /// <returns>An enumerable of <see cref="Tag"/>s.</returns> 110 110 public IEnumerable<Tag> Decompose(object obj) { 111 foreach (var accessor in GetStorableAccessors(obj )) {112 yield return new Tag(accessor.Name, accessor.Get( ));111 foreach (var accessor in GetStorableAccessors(obj.GetType())) { 112 yield return new Tag(accessor.Name, accessor.Get(obj)); 113 113 } 114 114 } … … 142 142 memberDict.Add(iter.Current.Name, iter.Current); 143 143 } 144 foreach (var accessor in GetStorableAccessors(instance )) {144 foreach (var accessor in GetStorableAccessors(instance.GetType())) { 145 145 if (memberDict.ContainsKey(accessor.Name)) { 146 accessor.Set( memberDict[accessor.Name].Value);146 accessor.Set(instance, memberDict[accessor.Name].Value); 147 147 } else if (accessor.DefaultValue != null) { 148 accessor.Set( accessor.DefaultValue);148 accessor.Set(instance, accessor.DefaultValue); 149 149 } 150 150 } … … 160 160 161 161 private static readonly object[] emptyArgs = new object[] { }; 162 private static readonly object[] trueArgs = new object[] { true }; 162 163 163 164 private sealed class HookDesignator { … … 171 172 } 172 173 173 private sealed class MemberCache : Dictionary<Type, IEnumerable<StorableMemberInfo>> { } 174 private sealed class AccessorListCache : Dictionary<Type, IEnumerable<DataMemberAccessor>> { } 175 private sealed class AccessorCache : Dictionary<MemberInfo, DataMemberAccessor> { } 174 176 175 177 #endregion … … 177 179 #region caches 178 180 179 private MemberCache storableMemberCache = new MemberCache(); 181 private AccessorListCache accessorListCache = new AccessorListCache(); 182 private AccessorCache accessorCache = new AccessorCache(); 180 183 181 184 private delegate object Constructor(); … … 191 194 #region attribute access 192 195 193 private IEnumerable<StorableMemberInfo> GetStorableMembers(Type type) { 194 lock (storableMemberCache) { 195 if (storableMemberCache.ContainsKey(type)) 196 return storableMemberCache[type]; 197 var storablesMembers = StorableReflection.GenerateStorableMembers(type); 198 storableMemberCache[type] = storablesMembers; 199 return storablesMembers; 196 private IEnumerable<DataMemberAccessor> GetStorableAccessors(Type type) { 197 lock (accessorListCache) { 198 if (accessorListCache.ContainsKey(type)) 199 return accessorListCache[type]; 200 var storableMembers = StorableReflection 201 .GenerateStorableMembers(type) 202 .Select(mi => GetMemberAccessor(mi)); 203 accessorListCache[type] = storableMembers; 204 return storableMembers; 205 } 206 } 207 208 private DataMemberAccessor GetMemberAccessor(StorableMemberInfo mi) { 209 lock (accessorCache) { 210 if (accessorCache.ContainsKey(mi.MemberInfo)) 211 return new DataMemberAccessor(accessorCache[mi.MemberInfo], mi.DisentangledName, mi.DefaultValue); 212 DataMemberAccessor dma = new DataMemberAccessor(mi.MemberInfo, mi.DisentangledName, mi.DefaultValue); 213 accessorCache[mi.MemberInfo] = dma; 214 return dma; 200 215 } 201 216 } … … 209 224 return c; 210 225 } 211 } 226 } 212 227 213 228 private Constructor GetDefaultConstructor(Type type) { 214 229 ConstructorInfo ci = type.GetConstructor(ALL_CONSTRUCTORS, null, Type.EmptyTypes, null); 215 230 if (ci == null) 216 return null; 231 return null; 217 232 DynamicMethod dm = new DynamicMethod("", typeof(object), null, type); 218 233 ILGenerator ilgen = dm.GetILGenerator(); … … 227 242 if (ci.GetParameters().Length != 1 || 228 243 ci.GetParameters()[0].ParameterType != typeof(bool)) 229 throw new PersistenceException("StorableConstructor must have exactly one argument of type bool"); 244 throw new PersistenceException("StorableConstructor must have exactly one argument of type bool"); 230 245 DynamicMethod dm = new DynamicMethod("", typeof(object), null, type); 231 246 ILGenerator ilgen = dm.GetILGenerator(); … … 237 252 } 238 253 return null; 239 } 240 241 private IEnumerable<DataMemberAccessor> GetStorableAccessors(object obj) { 242 return GetStorableMembers(obj.GetType()) 243 .Select(mi => new DataMemberAccessor(mi.MemberInfo, mi.DisentangledName, mi.DefaultValue, obj)); 244 } 254 } 245 255 246 256 private void InvokeHook(HookType hookType, object obj) {
Note: See TracChangeset
for help on using the changeset viewer.