Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/Descriptors/PropertyDescriptor.cs @ 6214

Last change on this file since 6214 was 6214, checked in by epitzer, 13 years ago

Streamline persistence reflection and store into persistable data structure for caching (#1530)

File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
24using System.Linq;
25using System.Reflection;
26using System.Text;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable.Descriptors {
30
31  /// <summary>
32  /// Describes a property of a type.
33  /// </summary>
34  [StorableClass]
35  public sealed class PropertyDescriptor : ITypeComponent {
36
37    /// <summary>
38    /// The type descriptor of the type who declared this property. This
39    /// can be different to the type where this property was obtained from
40    /// if it has been inherited.
41    /// </summary>
42    [Storable]
43    public TypeDescriptor DeclaringType { get; set; }
44
45    /// <summary>
46    /// The real name (i.e. in the object graph) of the property.
47    /// </summary>
48    [Storable]
49    public string RealName { get; set; }
50
51    /// <summary>
52    /// The name used for seralization (i.e. possibly prefixed with
53    /// type information to avoid confusion with equally named
54    /// shadowed properties) if different to the real name.
55    /// </summary>
56    [Storable]
57    public string StoredName { get; set; }
58
59    /// <summary>
60    /// The default value (if any) of the property if not found
61    /// during deserialziation.
62    /// </summary>
63    [Storable]
64    public object DefaultValue { get; set; }
65
66    /// <summary>
67    /// Whether this property has a read accessor.
68    /// </summary>
69    [Storable]
70    public bool CanRead { get; set; }
71
72    /// <summary>
73    /// Whether this property has a write accessor.
74    /// </summary>
75    [Storable]
76    public bool CanWrite { get; set; }
77
78    /// <summary>
79    /// The actual name used for serialization (either the stored name
80    /// if present, otherwise the real name).
81    /// </summary>
82    public string Name { get { return StoredName ?? RealName; } }
83
84    #region Activation
85    private PropertyInfo handle;
86    /// <summary>
87    /// The PropertyInfo object as obtained by reflection representing
88    /// the described type.
89    /// </summary>
90    public PropertyInfo Handle {
91      get {
92        try {
93          if (handle == null)
94            handle = DeclaringType.Handle.GetProperty(RealName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
95        } catch { }
96        return handle;
97      }
98      internal set {
99        handle = value;
100      }
101    }
102    #endregion
103
104    /// <summary>
105    /// The type who initially declared this property. This is usually the
106    /// first base type who declared this property even though it has been overridden
107    /// serveral times. If it has been newly declared using <code>new</code>
108    /// the type who shadowed it with new is returned.
109    /// </summary>
110    public Type DeclaringBaseType {
111      get {
112        if (CanRead)
113          return Handle.GetGetMethod().GetBaseDefinition().DeclaringType;
114        if (CanWrite)
115          return Handle.GetSetMethod().GetBaseDefinition().DeclaringType;
116        return Handle.DeclaringType;
117      }
118    }
119
120    [StorableConstructor]
121    private PropertyDescriptor(bool deserializing)  { }
122
123    /// <summary>
124    /// Create a new property descriptor.
125    /// </summary>
126    /// <param name="declaringType">The declaring type.</param>
127    /// <param name="propertyInfo">The reflection information.</param>
128    /// <param name="attribute">The storable attribute of this property (if any).</param>
129    public PropertyDescriptor(TypeDescriptor declaringType, PropertyInfo propertyInfo, StorableAttribute attribute) {
130      DeclaringType = declaringType;
131      RealName = propertyInfo.Name;
132      if (attribute != null) {
133        StoredName = attribute.Name;
134        DefaultValue = attribute.DefaultValue;
135      }
136      CanRead = propertyInfo.CanRead;
137      CanWrite = propertyInfo.CanWrite;
138      Handle = propertyInfo;
139    }
140
141    /// <summary>
142    /// Create a shallow copy of this property descriptor.
143    /// </summary>
144    public object Clone() {
145      var clone = new PropertyDescriptor(false);
146      clone.DeclaringType = DeclaringType;
147      clone.RealName = RealName;
148      clone.StoredName = StoredName;
149      clone.DefaultValue = DefaultValue;
150      clone.CanRead = CanRead;
151      clone.CanWrite = CanWrite;
152      clone.handle = handle;
153      return clone;
154    }
155
156    /// <summary>
157    /// Create a string representation of this property descriptor.
158    /// </summary>
159    public override string ToString() {
160      StringBuilder sb = new StringBuilder();
161      sb.Append(base.ToString());
162      if (CanRead && !CanWrite)
163        sb.Append(" {RO}");
164      else if (CanWrite && !CanRead)
165        sb.Append(" {WO}");
166      else if (CanRead && CanWrite)
167        sb.Append(" {R/W}");
168      else
169        sb.Append(" {!!! MUTE !!!}");
170      return sb.ToString();
171    }
172
173    /// <summary>
174    /// From the given object obtain the property value that
175    /// is described by this property descriptor.
176    /// </summary>
177    public object GetValue(object o) {
178      return Handle.GetValue(o, null);
179    }
180
181    /// <summary>
182    /// From the given object set the property value that
183    /// is described by this property descriptor.
184    /// </summary>
185    public void SetValue(object o, object value) {
186      Handle.SetValue(o, value, null);
187    }
188  }
189
190}
Note: See TracBrowser for help on using the repository browser.