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