Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Core/DataMemberAccessor.cs @ 3205

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

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

File size: 4.0 KB
Line 
1using System;
2using System.Reflection;
3using HeuristicLab.Persistence.Core;
4
5namespace HeuristicLab.Persistence.Core {
6
7  /// <summary>
8  /// Encapsulation and abstraction for access a data member of an object
9  /// regardless of it being a property or field. Additionally a
10  /// default value and an alternate name can be specified.
11  /// </summary>
12  public class DataMemberAccessor {
13
14    /// <summary>
15    /// The function to get the value of the data member.
16    /// </summary>
17    public readonly Func<object> Get;
18
19    /// <summary>
20    /// The function to set the value of the data member.
21    /// </summary>
22    public readonly Action<object> Set;
23
24    /// <summary>
25    /// The name of the data member.
26    /// </summary>
27    public readonly string Name;
28
29    /// <summary>
30    /// The default value of the data member, can remain <c>null</c>
31    /// if no default value. If left null, this will also leave the
32    /// default value for value types (e.g. 0 for <c>int</c>).
33    /// </summary>
34    public readonly object DefaultValue;
35
36
37    /// <summary>
38    /// Create a <see cref="DataMemberAccessor"/> from a FieldInfo or
39    /// PropertyInfo for the give object.
40    /// </summary>
41    /// <param name="memberInfo">The member info.</param>
42    /// <param name="name">The name.</param>
43    /// <param name="defaultvalue">The defaultvalue.</param>
44    /// <param name="obj">The object.</param>
45    public DataMemberAccessor(MemberInfo memberInfo, string name, object defaultvalue, object obj) {
46      Name = name;
47      DefaultValue = defaultvalue;
48      if (memberInfo.MemberType == MemberTypes.Field) {
49        FieldInfo fieldInfo = (FieldInfo)memberInfo;
50        Get = () => fieldInfo.GetValue(obj);
51        Set = value => fieldInfo.SetValue(obj, value);
52      } else if (memberInfo.MemberType == MemberTypes.Property) {
53        PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
54        if (!propertyInfo.CanRead || !propertyInfo.CanWrite) {
55          throw new PersistenceException(
56            "Storable properties must implement both a Get and a Set Accessor. ");
57        }
58        Get = () => propertyInfo.GetValue(obj, null);
59        Set = value => propertyInfo.SetValue(obj, value, null);
60      } else {
61        throw new PersistenceException(
62          "The Storable attribute can only be applied to fields and properties.");
63      }
64    }
65
66    /// <summary>
67    /// Wrap existing getter and setter functions.
68    /// </summary>
69    /// <param name="name">The name.</param>
70    /// <param name="defaultValue">The default value.</param>
71    /// <param name="getter">The getter.</param>
72    /// <param name="setter">The setter.</param>
73    public DataMemberAccessor(string name, object defaultValue,
74        Func<object> getter, Action<object> setter) {
75      Name = name;
76      DefaultValue = defaultValue;
77      Get = getter;
78      Set = setter;
79    }
80
81    /// <summary>
82    /// Create an empty accessor that just encapsulates an object
83    /// without access.
84    /// </summary>
85    /// <param name="o">The object</param>
86    public DataMemberAccessor(object o) {
87      Name = null;
88      DefaultValue = null;
89      Get = () => o;
90      Set = null;
91    }
92
93    /// <summary>
94    /// Create an empty accessor that just encapsulates an object
95    /// without access.
96    /// </summary>
97    /// <param name="o">The object</param>
98    /// <param name="name">The object's name.</param>
99    public DataMemberAccessor(object o, string name) {
100      Name = name;
101      DefaultValue = null;
102      Get = () => o;
103      Set = null;
104    }
105
106    /// <summary>
107    /// Returns a <see cref="System.String"/> that represents this instance.
108    /// </summary>
109    /// <returns>
110    /// A <see cref="System.String"/> that represents this instance.
111    /// </returns>
112    public override string ToString() {
113      return String.Format("DataMemberAccessor({0}, {1}, {2}, {3})",
114        Name,
115        DefaultValue ?? "<null>",
116        Get.Method, Set.Method);
117    }
118  }
119
120}
Note: See TracBrowser for help on using the repository browser.