Free cookie consent management tool by TermsFeed Policy Generator

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

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

collect all storable caches into storable serializer to reduce prolonged memory usage (#548)

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