Free cookie consent management tool by TermsFeed Policy Generator

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

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

Scrap Storable-based cloning & reorganize namespaces. (#506)

File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4
5namespace HeuristicLab.Persistence.Core {
6
7  [AttributeUsage(
8    AttributeTargets.Field | AttributeTargets.Property,
9    AllowMultiple=false,
10    Inherited=false)]
11  public class StorableAttribute : Attribute {
12
13    public string Name { get; set; }
14    public object DefaultValue { get; set; }
15
16    private const BindingFlags instanceMembers =
17      BindingFlags.Instance |
18      BindingFlags.Public |
19      BindingFlags.NonPublic;
20   
21    public static IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>> GetStorableMembers(Type type) {
22      return GetStorableMembers(type, true);     
23    }
24
25    public static IEnumerable<KeyValuePair<StorableAttribute, MemberInfo>>
26        GetStorableMembers(Type type, bool inherited) {
27      if (type.BaseType != null)
28        foreach ( var pair in GetStorableMembers(type.BaseType) )
29          yield return pair;
30      foreach (MemberInfo memberInfo in type.GetMembers(instanceMembers)) {
31        foreach (object attribute in memberInfo.GetCustomAttributes(false)) {         
32          StorableAttribute storableAttribute =
33            attribute as StorableAttribute;
34          if (storableAttribute != null) {
35            yield return new KeyValuePair<StorableAttribute, MemberInfo>(storableAttribute, memberInfo);           
36          }
37        }
38      }     
39    }
40
41    public static Dictionary<string, DataMemberAccessor> GetAutostorableAccessors(object obj) {
42      Dictionary<string, DataMemberAccessor> storableAccessors =
43        new Dictionary<string, DataMemberAccessor>();
44      foreach (KeyValuePair<StorableAttribute, MemberInfo> pair in GetStorableMembers(obj.GetType())) {
45        storableAccessors.Add(pair.Value.Name,
46          new DataMemberAccessor(pair.Value, pair.Key, obj));
47      }           
48      return storableAccessors;
49    }   
50  }
51}
Note: See TracBrowser for help on using the repository browser.