Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableReflection.cs @ 3553

Last change on this file since 3553 was 3553, checked in by epitzer, 15 years ago

replace repeated calls through reflection with generated code for a twofold speedup (#548)

File size: 7.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Persistence.Interfaces;
5using HeuristicLab.Persistence.Core;
6using System.Reflection;
7using HeuristicLab.Persistence.Auxiliary;
8using System.Text;
9using System.Reflection.Emit;
10
11namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable {
12
13  internal static class StorableReflection {
14
15    private const BindingFlags DECLARED_INSTANCE_MEMBERS =
16      BindingFlags.Instance |
17      BindingFlags.Public |
18      BindingFlags.NonPublic |
19      BindingFlags.DeclaredOnly;
20
21    private delegate void HookWrapper<T>(T o);
22    public delegate void Hook(object o);
23
24    public static IEnumerable<StorableMemberInfo> GenerateStorableMembers(Type type) {
25      var storableMembers = new List<StorableMemberInfo>();
26      if (type.BaseType != null)
27        storableMembers.AddRange(GenerateStorableMembers(type.BaseType));
28
29      var storableClassAttribute = GetStorableClassAttribute(type);
30      if (storableClassAttribute != null) {
31        switch (storableClassAttribute.Type) {
32          case StorableClassType.MarkedOnly:
33            AddMarkedMembers(type, storableMembers); break;
34          case StorableClassType.AllFields:
35            AddAll(type, MemberTypes.Field, storableMembers); break;
36          case StorableClassType.AllProperties:
37            AddAll(type, MemberTypes.Property, storableMembers); break;
38          case StorableClassType.AllFieldsAndAllProperties:
39            AddAll(type, MemberTypes.Field | MemberTypes.Property, storableMembers); break;
40          default:
41            throw new PersistenceException("unsupported [StorableClassType]: " + storableClassAttribute.Type);
42        }
43      }
44
45      return DisentangleNameMapping(storableMembers);
46    }
47
48    public static bool IsEmptyOrStorableType(Type type, bool recursive) {     
49      if (!HasStorableClassAttribute(type) && !IsEmptyType(type, false)) return false;
50      return !recursive || type.BaseType == null || IsEmptyOrStorableType(type.BaseType, true);
51    }
52
53    public static IEnumerable<Hook> CollectHooks(HookType hookType, Type type) {
54      if (type.BaseType != null)
55        foreach (var mi in CollectHooks(hookType, type.BaseType))
56          yield return mi;
57      foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) {
58        foreach (StorableHookAttribute hook in memberInfo.GetCustomAttributes(typeof(StorableHookAttribute), false)) {
59          if (hook != null && hook.HookType == hookType) {
60            MethodInfo methodInfo = memberInfo as MethodInfo;
61            if (memberInfo.MemberType != MemberTypes.Method || memberInfo == null)
62              throw new ArgumentException("Storable hooks must be methods");       
63            DynamicMethod dm = new DynamicMethod("", null, new[] { typeof(object) }, type);
64            ILGenerator ilgen = dm.GetILGenerator();
65            ilgen.Emit(OpCodes.Ldarg_1);
66            ilgen.Emit(OpCodes.Call, methodInfo);
67            ilgen.Emit(OpCodes.Ret);
68            yield return (Hook)dm.CreateDelegate(typeof(Hook));
69          }
70        }
71      }
72    }
73
74    #region [Storable] helpers
75
76    private static void AddMarkedMembers(Type type, List<StorableMemberInfo> storableMembers) {
77      foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) {
78        foreach (StorableAttribute attribute in memberInfo.GetCustomAttributes(typeof(StorableAttribute), false)) {
79          storableMembers.Add(new StorableMemberInfo(attribute, memberInfo));
80        }
81      }
82    }
83
84    private static void AddAll(Type type, MemberTypes memberTypes, List<StorableMemberInfo> storableMembers) {
85      foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) {
86        if ((memberInfo.MemberType & memberTypes) == memberInfo.MemberType &&
87            !memberInfo.Name.StartsWith("<") &&
88            !memberInfo.Name.EndsWith("k__BackingField"))
89          storableMembers.Add(new StorableMemberInfo(memberInfo));
90      }
91    }
92
93
94    private static IEnumerable<StorableMemberInfo> DisentangleNameMapping(
95        IEnumerable<StorableMemberInfo> storableMemberInfos) {
96      var nameGrouping = new Dictionary<string, List<StorableMemberInfo>>();
97      foreach (StorableMemberInfo storable in storableMemberInfos) {
98        if (!nameGrouping.ContainsKey(storable.MemberInfo.Name))
99          nameGrouping[storable.MemberInfo.Name] = new List<StorableMemberInfo>();
100        nameGrouping[storable.MemberInfo.Name].Add(storable);
101      }
102      var memberInfos = new List<StorableMemberInfo>();
103      foreach (var storableMemberInfoGroup in nameGrouping.Values) {
104        if (storableMemberInfoGroup.Count == 1) {
105          storableMemberInfoGroup[0].SetDisentangledName(storableMemberInfoGroup[0].MemberInfo.Name);
106          memberInfos.Add(storableMemberInfoGroup[0]);
107        } else if (storableMemberInfoGroup[0].MemberInfo.MemberType == MemberTypes.Field) {
108          foreach (var storableMemberInfo in storableMemberInfoGroup) {
109            storableMemberInfo.SetDisentangledName(storableMemberInfo.FullyQualifiedMemberName);
110            memberInfos.Add(storableMemberInfo);
111          }
112        } else {
113          memberInfos.AddRange(MergePropertyAccessors(storableMemberInfoGroup));
114        }
115      }
116      return memberInfos;
117    }
118
119    private static IEnumerable<StorableMemberInfo> MergePropertyAccessors(List<StorableMemberInfo> members) {
120      var uniqueAccessors = new Dictionary<Type, StorableMemberInfo>();
121      foreach (var member in members)
122        uniqueAccessors[member.GetPropertyDeclaringBaseType()] = member;
123      if (uniqueAccessors.Count == 1) {
124        var storableMemberInfo = uniqueAccessors.Values.First();
125        storableMemberInfo.SetDisentangledName(storableMemberInfo.MemberInfo.Name);
126        yield return storableMemberInfo;
127      } else {
128        foreach (var attribute in uniqueAccessors.Values) {
129          attribute.SetDisentangledName(attribute.FullyQualifiedMemberName);
130          yield return attribute;
131        }
132      }
133    }
134
135    #endregion
136
137    #region [StorableClass] helpers
138
139    private static StorableClassAttribute GetStorableClassAttribute(Type type) {
140      return (StorableClassAttribute)type
141        .GetCustomAttributes(typeof(StorableClassAttribute), false)
142        .SingleOrDefault();
143    }
144
145    private static bool HasStorableClassAttribute(Type type) {
146      return type.GetCustomAttributes(typeof(StorableClassAttribute), false).Length > 0;
147    }
148
149    #endregion
150
151    #region other helpers
152
153    private static bool IsEmptyType(Type type, bool recursive) {
154      foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) {
155        if (IsModifiableMember(memberInfo)) return false;
156      }
157      return !recursive || type.BaseType == null || IsEmptyType(type.BaseType, true);
158    }
159
160    private static bool IsModifiableMember(MemberInfo memberInfo) {
161      return memberInfo.MemberType == MemberTypes.Field && IsModifiableField((FieldInfo)memberInfo) ||
162             memberInfo.MemberType == MemberTypes.Property && IsModifiableProperty((PropertyInfo)memberInfo);
163    }
164
165    private static bool IsModifiableField(FieldInfo fi) {
166      return !fi.IsLiteral && !fi.IsInitOnly;
167    }
168
169    private static bool IsModifiableProperty(PropertyInfo pi) {
170      return pi.CanWrite;
171    }
172
173    #endregion
174
175  }
176}
Note: See TracBrowser for help on using the repository browser.