- Timestamp:
- 03/10/10 15:27:10 (15 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableAttribute.cs
r1960 r2991 88 88 89 89 public static IEnumerable<StorableMemberInfo> GetStorableMembers(Type type, bool inherited) { 90 var query = new TypeQuery(type, inherited); 91 if (memberCache.ContainsKey(query)) 92 return memberCache[query]; 93 var storablesMembers = GenerateStorableMembers(type, inherited); 94 memberCache[query] = storablesMembers; 95 return storablesMembers; 90 lock (memberCache) { 91 var query = new TypeQuery(type, inherited); 92 if (memberCache.ContainsKey(query)) 93 return memberCache[query]; 94 var storablesMembers = GenerateStorableMembers(type, inherited); 95 memberCache[query] = storablesMembers; 96 return storablesMembers; 97 } 96 98 } 97 99 … … 101 103 storableMembers.AddRange(GenerateStorableMembers(type.BaseType, true)); 102 104 foreach (MemberInfo memberInfo in type.GetMembers(instanceMembers)) { 103 foreach (object attribute in memberInfo.GetCustomAttributes(false)) { 104 StorableAttribute storableAttribute = attribute as StorableAttribute; 105 if (storableAttribute != null) { 106 storableMembers.Add(new StorableMemberInfo(storableAttribute, memberInfo)); 107 } 105 foreach (StorableAttribute attribute in memberInfo.GetCustomAttributes(typeof(StorableAttribute), false)) { 106 storableMembers.Add(new StorableMemberInfo(attribute, memberInfo)); 108 107 } 109 108 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableConstructorAttribute.cs
r2983 r2991 12 12 13 13 private static readonly BindingFlags allConstructors = 14 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; 14 BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; 15 15 16 private static readonly object[] defaultArgs = new object[] { true }; 16 private static Dictionary<Type, ConstructorInfo> constructorCache = 17 new Dictionary<Type, ConstructorInfo>(); 17 18 18 public static object CallStorableConstructor(Type type) { 19 foreach (ConstructorInfo constructorInfo in type.GetConstructors(allConstructors)) { 20 if (constructorInfo 21 .GetCustomAttributes(typeof(StorableConstructorAttribute), false).Length > 0) { 22 if (constructorInfo.GetParameters().Length != 1 || 23 constructorInfo.GetParameters()[0].ParameterType != typeof(bool)) 24 throw new PersistenceException("StorableConstructor must have exactly one argument of type bool"); 25 return constructorInfo.Invoke(defaultArgs); 19 public static ConstructorInfo GetStorableConstructor(Type type) { 20 lock (constructorCache) { 21 if (constructorCache.ContainsKey(type)) 22 return constructorCache[type]; 23 foreach (ConstructorInfo ci in type.GetConstructors(allConstructors)) { 24 if (ci.GetCustomAttributes(typeof(StorableConstructorAttribute), false).Length > 0) { 25 if (ci.GetParameters().Length != 1 || 26 ci.GetParameters()[0].ParameterType != typeof(bool)) 27 throw new PersistenceException("StorableConstructor must have exactly one argument of type bool"); 28 constructorCache[type] = ci; 29 return ci; 30 } 26 31 } 32 constructorCache[type] = null; 33 return null; 27 34 } 28 return null;29 35 } 30 31 36 } 32 37 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableHookAttribute.cs
r2983 r2991 11 11 [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)] 12 12 public sealed class StorableHookAttribute : Attribute { 13 14 private sealed class HookDesignator { 15 public Type Type { get; set; } 16 public HookType HookType { get; set; } 17 public HookDesignator() { } 18 public HookDesignator(Type type, HookType hookType) { 19 Type = type; 20 HookType = HookType; 21 } 22 } 13 23 14 24 private readonly HookType hookType; … … 26 36 private static readonly object[] emptyArgs = new object[] { }; 27 37 38 private static Dictionary<HookDesignator, List<MethodInfo>> hookCache = 39 new Dictionary<HookDesignator, List<MethodInfo>>(); 40 28 41 public static void InvokeHook(HookType hookType, object obj) { 29 42 if (obj == null) 30 43 throw new ArgumentNullException("Cannot invoke hooks on null"); 31 Type type = obj.GetType(); 44 foreach (MethodInfo mi in GetHooks(hookType, obj.GetType())) { 45 mi.Invoke(obj, emptyArgs); 46 } 47 } 48 49 private static IEnumerable<MethodInfo> GetHooks(HookType hookType, Type type) { 50 lock (hookCache) { 51 List<MethodInfo> hooks; 52 var designator = new HookDesignator(type, hookType); 53 hookCache.TryGetValue(designator, out hooks); 54 if (hooks != null) 55 return hooks; 56 hooks = new List<MethodInfo>(CollectHooks(hookType, type)); 57 hookCache.Add(designator, hooks); 58 return hooks; 59 } 60 } 61 62 private static IEnumerable<MethodInfo> CollectHooks(HookType hookType, Type type) { 32 63 foreach (MemberInfo memberInfo in type.GetMembers(instanceMembers)) { 33 foreach (StorableHookAttribute hook in memberInfo.GetCustomAttributes(typeof(StorableHookAttribute), false)) { 64 foreach (StorableHookAttribute hook in memberInfo.GetCustomAttributes(typeof(StorableHookAttribute), false)) { 34 65 if (hook != null && hook.HookType == hookType) { 35 66 MethodInfo methodInfo = memberInfo as MethodInfo; 36 67 if (memberInfo.MemberType != MemberTypes.Method || memberInfo == null) 37 68 throw new ArgumentException("Storable hooks must be methods"); 38 methodInfo.Invoke(obj, emptyArgs);69 yield return methodInfo; 39 70 } 40 71 } -
trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs
r2990 r2991 19 19 if (!ReflectionTools.HasDefaultConstructor(type)) 20 20 return false; 21 while (type != null) { 21 while (type != null) { 22 22 if (StorableAttribute.GetStorableMembers(type, false).Count() == 0 && 23 23 !EmptyStorableClassAttribute.IsEmptyStorable(type)) … … 39 39 } 40 40 41 private static readonly object[] defaultArgs = new object[] { true }; 42 41 43 public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) { 42 44 try { 43 object instance = StorableConstructorAttribute.CallStorableConstructor(type); 44 if (instance == null) 45 instance = Activator.CreateInstance(type, true); 46 return instance; 45 ConstructorInfo constructor = StorableConstructorAttribute.GetStorableConstructor(type); 46 return constructor != null ? constructor.Invoke(defaultArgs) : Activator.CreateInstance(type, true); 47 47 } catch (TargetInvocationException x) { 48 48 throw new PersistenceException(
Note: See TracChangeset
for help on using the changeset viewer.