source: branches/New Persistence Exploration/Persistence/Persistence/Core/StorableAttribute.cs @ 1404

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

New attribute for legitimately empty but storable classes. (#506)

File size: 3.1 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4
5namespace HeuristicLab.Persistence.Core {
6
7 
8  [AttributeUsage(
9    AttributeTargets.Class,
10    AllowMultiple=false,
11    Inherited=false)]
12  public class EmptyStorableClassAttribute : Attribute {
13    private static readonly Dictionary<Type, bool> emptyTypeInfo = new Dictionary<Type, bool>();
14    public static bool IsEmpyStorable(object o) {
15      Type type = o.GetType();
16      if (emptyTypeInfo.ContainsKey(type))
17        return emptyTypeInfo[type];
18      foreach (var attribute in o.GetType().GetCustomAttributes(false)) {
19        EmptyStorableClassAttribute empty = attribute as EmptyStorableClassAttribute;
20        if (empty != null) {
21          emptyTypeInfo.Add(type, true);
22          return true;
23        }
24      }
25      int nFields = 0;
26      foreach ( MemberInfo memberInfo in type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ) {
27        if (memberInfo.MemberType == MemberTypes.Field ||
28          memberInfo.MemberType == MemberTypes.Property)
29          nFields += 1;
30      }
31      if (nFields == 0) {
32        emptyTypeInfo.Add(type, true);
33        return true;
34      }
35      emptyTypeInfo.Add(type, false);
36      return false;
37    }
38  }
39
40  [AttributeUsage(
41    AttributeTargets.Field | AttributeTargets.Property,
42    AllowMultiple=false,
43    Inherited=false)]
44  public class StorableAttribute : Attribute {
45
46    public string Name { get; set; }
47    public object DefaultValue { get; set; }
48
49    private const BindingFlags instanceMembers =
50      BindingFlags.Instance |
51      BindingFlags.Public |
52      BindingFlags.NonPublic;
53   
54    public static IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>> GetStorableMembers(Type type) {
55      return GetStorableMembers(type, true);     
56    }
57
58    public static IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>>
59        GetStorableMembers(Type type, bool inherited) {
60      if (type.BaseType != null)
61        foreach ( var pair in GetStorableMembers(type.BaseType) )
62          yield return pair;
63      foreach (MemberInfo memberInfo in type.GetMembers(instanceMembers)) {
64        foreach (object attribute in memberInfo.GetCustomAttributes(false)) {         
65          StorableAttribute storableAttribute =
66            attribute as StorableAttribute;
67          if (storableAttribute != null) {
68            yield return new KeyValuePair<StorableAttribute, MemberInfo>(storableAttribute, memberInfo);           
69          }
70        }
71      }     
72    }
73
74    public static Dictionary<string, DataMemberAccessor> GetAutostorableAccessors(object obj) {
75      Dictionary<string, DataMemberAccessor> storableAccessors =
76        new Dictionary<string, DataMemberAccessor>();
77      foreach (KeyValuePair<StorableAttribute, MemberInfo> pair in GetStorableMembers(obj.GetType())) {
78        storableAccessors.Add(pair.Value.Name,
79          new DataMemberAccessor(pair.Value, pair.Key, obj));
80      }           
81      return storableAccessors;
82    }   
83  }
84}
Note: See TracBrowser for help on using the repository browser.