Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3743 was 3743, checked in by gkronber, 14 years ago

Fixed GPL license headers #893

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Reflection;
24using HeuristicLab.Persistence.Core;
25
26namespace HeuristicLab.Persistence.Core {
27
28  /// <summary>
29  /// Encapsulation and abstraction for access a data member of an object
30  /// regardless of it being a property or field. Additionally a
31  /// default value and an alternate name can be specified.
32  /// </summary>
33  public class DataMemberAccessor {
34
35    /// <summary>
36    /// The function to get the value of the data member.
37    /// </summary>
38    public readonly Func<object> Get;
39
40    /// <summary>
41    /// The function to set the value of the data member.
42    /// </summary>
43    public readonly Action<object> Set;
44
45    /// <summary>
46    /// The name of the data member.
47    /// </summary>
48    public readonly string Name;
49
50    /// <summary>
51    /// The default value of the data member, can remain <c>null</c>
52    /// if no default value. If left null, this will also leave the
53    /// default value for value types (e.g. 0 for <c>int</c>).
54    /// </summary>
55    public readonly object DefaultValue;
56
57
58    /// <summary>
59    /// Create a <see cref="DataMemberAccessor"/> from a FieldInfo or
60    /// PropertyInfo for the give object.
61    /// </summary>
62    /// <param name="memberInfo">The member info.</param>
63    /// <param name="name">The name.</param>
64    /// <param name="defaultvalue">The defaultvalue.</param>
65    /// <param name="obj">The object.</param>
66    public DataMemberAccessor(MemberInfo memberInfo, string name, object defaultvalue, object obj) {
67      Name = name;
68      DefaultValue = defaultvalue;
69      if (memberInfo.MemberType == MemberTypes.Field) {
70        FieldInfo fieldInfo = (FieldInfo)memberInfo;
71        Get = () => fieldInfo.GetValue(obj);
72        Set = value => fieldInfo.SetValue(obj, value);
73      } else if (memberInfo.MemberType == MemberTypes.Property) {
74        PropertyInfo propertyInfo = (PropertyInfo)memberInfo;
75        if (!propertyInfo.CanRead || !propertyInfo.CanWrite) {
76          throw new PersistenceException(
77            "Storable properties must implement both a Get and a Set Accessor. ");
78        }
79        Get = () => propertyInfo.GetValue(obj, null);
80        Set = value => propertyInfo.SetValue(obj, value, null);
81      } else {
82        throw new PersistenceException(
83          "The Storable attribute can only be applied to fields and properties.");
84      }
85    }
86
87    /// <summary>
88    /// Wrap existing getter and setter functions.
89    /// </summary>
90    /// <param name="name">The name.</param>
91    /// <param name="defaultValue">The default value.</param>
92    /// <param name="getter">The getter.</param>
93    /// <param name="setter">The setter.</param>
94    public DataMemberAccessor(string name, object defaultValue,
95        Func<object> getter, Action<object> setter) {
96      Name = name;
97      DefaultValue = defaultValue;
98      Get = getter;
99      Set = setter;
100    }
101
102    /// <summary>
103    /// Create an empty accessor that just encapsulates an object
104    /// without access.
105    /// </summary>
106    /// <param name="o">The object</param>
107    public DataMemberAccessor(object o) {
108      Name = null;
109      DefaultValue = null;
110      Get = () => o;
111      Set = null;
112    }
113
114    /// <summary>
115    /// Create an empty accessor that just encapsulates an object
116    /// without access.
117    /// </summary>
118    /// <param name="o">The object</param>
119    /// <param name="name">The object's name.</param>
120    public DataMemberAccessor(object o, string name) {
121      Name = name;
122      DefaultValue = null;
123      Get = () => o;
124      Set = null;
125    }
126
127    /// <summary>
128    /// Returns a <see cref="System.String"/> that represents this instance.
129    /// </summary>
130    /// <returns>
131    /// A <see cref="System.String"/> that represents this instance.
132    /// </returns>
133    public override string ToString() {
134      return String.Format("DataMemberAccessor({0}, {1}, {2}, {3})",
135        Name,
136        DefaultValue ?? "<null>",
137        Get.Method, Set.Method);
138    }
139  }
140
141}
Note: See TracBrowser for help on using the repository browser.