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

Last change on this file since 3554 was 3554, checked in by epitzer, 12 years ago

circumvent code generation for hooks on 64bit systems, this is a bug in the CLR (#548)

File size: 7.6 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    private static object[] emptyArgs = new object[0];
54
55    public static IEnumerable<Hook> CollectHooks(HookType hookType, Type type) {
56      if (type.BaseType != null)
57        foreach (var mi in CollectHooks(hookType, type.BaseType))
58          yield return mi;
59      foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) {
60        foreach (StorableHookAttribute hook in memberInfo.GetCustomAttributes(typeof(StorableHookAttribute), false)) {
61          if (hook != null && hook.HookType == hookType) {
62            MethodInfo methodInfo = memberInfo as MethodInfo;
63            if (memberInfo.MemberType != MemberTypes.Method || memberInfo == null)
64              throw new ArgumentException("Storable hooks must be methods");
65            if (System.IntPtr.Size == 4) {
66              DynamicMethod dm = new DynamicMethod("", null, new[] { typeof(object) }, type);
67              ILGenerator ilgen = dm.GetILGenerator();
68              ilgen.Emit(OpCodes.Ldarg_1);
69              ilgen.Emit(OpCodes.Call, methodInfo);
70              ilgen.Emit(OpCodes.Ret);
71              yield return (Hook)dm.CreateDelegate(typeof(Hook));
72            } else {
73              yield return new Hook(o => methodInfo.Invoke(o, emptyArgs));
74            }
75          }
76        }
77      }
78    }
79
80    #region [Storable] helpers
81
82    private static void AddMarkedMembers(Type type, List<StorableMemberInfo> storableMembers) {
83      foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) {
84        foreach (StorableAttribute attribute in memberInfo.GetCustomAttributes(typeof(StorableAttribute), false)) {
85          storableMembers.Add(new StorableMemberInfo(attribute, memberInfo));
86        }
87      }
88    }
89
90    private static void AddAll(Type type, MemberTypes memberTypes, List<StorableMemberInfo> storableMembers) {
91      foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) {
92        if ((memberInfo.MemberType & memberTypes) == memberInfo.MemberType &&
93            !memberInfo.Name.StartsWith("<") &&
94            !memberInfo.Name.EndsWith("k__BackingField"))
95          storableMembers.Add(new StorableMemberInfo(memberInfo));
96      }
97    }
98
99
100    private static IEnumerable<StorableMemberInfo> DisentangleNameMapping(
101        IEnumerable<StorableMemberInfo> storableMemberInfos) {
102      var nameGrouping = new Dictionary<string, List<StorableMemberInfo>>();
103      foreach (StorableMemberInfo storable in storableMemberInfos) {
104        if (!nameGrouping.ContainsKey(storable.MemberInfo.Name))
105          nameGrouping[storable.MemberInfo.Name] = new List<StorableMemberInfo>();
106        nameGrouping[storable.MemberInfo.Name].Add(storable);
107      }
108      var memberInfos = new List<StorableMemberInfo>();
109      foreach (var storableMemberInfoGroup in nameGrouping.Values) {
110        if (storableMemberInfoGroup.Count == 1) {
111          storableMemberInfoGroup[0].SetDisentangledName(storableMemberInfoGroup[0].MemberInfo.Name);
112          memberInfos.Add(storableMemberInfoGroup[0]);
113        } else if (storableMemberInfoGroup[0].MemberInfo.MemberType == MemberTypes.Field) {
114          foreach (var storableMemberInfo in storableMemberInfoGroup) {
115            storableMemberInfo.SetDisentangledName(storableMemberInfo.FullyQualifiedMemberName);
116            memberInfos.Add(storableMemberInfo);
117          }
118        } else {
119          memberInfos.AddRange(MergePropertyAccessors(storableMemberInfoGroup));
120        }
121      }
122      return memberInfos;
123    }
124
125    private static IEnumerable<StorableMemberInfo> MergePropertyAccessors(List<StorableMemberInfo> members) {
126      var uniqueAccessors = new Dictionary<Type, StorableMemberInfo>();
127      foreach (var member in members)
128        uniqueAccessors[member.GetPropertyDeclaringBaseType()] = member;
129      if (uniqueAccessors.Count == 1) {
130        var storableMemberInfo = uniqueAccessors.Values.First();
131        storableMemberInfo.SetDisentangledName(storableMemberInfo.MemberInfo.Name);
132        yield return storableMemberInfo;
133      } else {
134        foreach (var attribute in uniqueAccessors.Values) {
135          attribute.SetDisentangledName(attribute.FullyQualifiedMemberName);
136          yield return attribute;
137        }
138      }
139    }
140
141    #endregion
142
143    #region [StorableClass] helpers
144
145    private static StorableClassAttribute GetStorableClassAttribute(Type type) {
146      return (StorableClassAttribute)type
147        .GetCustomAttributes(typeof(StorableClassAttribute), false)
148        .SingleOrDefault();
149    }
150
151    private static bool HasStorableClassAttribute(Type type) {
152      return type.GetCustomAttributes(typeof(StorableClassAttribute), false).Length > 0;
153    }
154
155    #endregion
156
157    #region other helpers
158
159    private static bool IsEmptyType(Type type, bool recursive) {
160      foreach (MemberInfo memberInfo in type.GetMembers(DECLARED_INSTANCE_MEMBERS)) {
161        if (IsModifiableMember(memberInfo)) return false;
162      }
163      return !recursive || type.BaseType == null || IsEmptyType(type.BaseType, true);
164    }
165
166    private static bool IsModifiableMember(MemberInfo memberInfo) {
167      return memberInfo.MemberType == MemberTypes.Field && IsModifiableField((FieldInfo)memberInfo) ||
168             memberInfo.MemberType == MemberTypes.Property && IsModifiableProperty((PropertyInfo)memberInfo);
169    }
170
171    private static bool IsModifiableField(FieldInfo fi) {
172      return !fi.IsLiteral && !fi.IsInitOnly;
173    }
174
175    private static bool IsModifiableProperty(PropertyInfo pi) {
176      return pi.CanWrite;
177    }
178
179    #endregion
180
181  }
182}
Note: See TracBrowser for help on using the repository browser.