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 | using System;
|
---|
22 | using System.Reflection;
|
---|
23 | using System.Text;
|
---|
24 |
|
---|
25 | namespace HeuristicLab.Persistence.Default.CompositeSerializers.Storable.Descriptors {
|
---|
26 |
|
---|
27 | /// <summary>
|
---|
28 | /// Describes the field of a type.
|
---|
29 | /// </summary>
|
---|
30 | [StorableClass]
|
---|
31 | public sealed class FieldDescriptor : ITypeComponent, ICloneable {
|
---|
32 |
|
---|
33 | #region Fields & Properties
|
---|
34 |
|
---|
35 | /// <summary>
|
---|
36 | /// The type that declared this field.
|
---|
37 | /// </summary>
|
---|
38 | [Storable]
|
---|
39 | public TypeDescriptor DeclaringType { get; set; }
|
---|
40 |
|
---|
41 | /// <summary>
|
---|
42 | /// The field name in the object graph as obtained by reflection.
|
---|
43 | /// </summary>
|
---|
44 | [Storable]
|
---|
45 | public string RealName { get; set; }
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | /// The name used for storing this field if different from the
|
---|
49 | /// the real name.
|
---|
50 | /// </summary>
|
---|
51 | [Storable]
|
---|
52 | public string StoredName { get; set; }
|
---|
53 |
|
---|
54 | /// <summary>
|
---|
55 | /// The default value of this field (if any) when no value
|
---|
56 | /// was found during deserialization.
|
---|
57 | /// </summary>
|
---|
58 | [Storable]
|
---|
59 | public object DefaultValue { get; set; }
|
---|
60 |
|
---|
61 | /// <summary>
|
---|
62 | /// The name used for (de)serialization which is either the
|
---|
63 | /// StoredName (if present) or otherwise the RealName.
|
---|
64 | /// </summary>
|
---|
65 | public string Name { get { return StoredName ?? RealName; } }
|
---|
66 |
|
---|
67 | private FieldInfo handle;
|
---|
68 | /// <summary>
|
---|
69 | /// The FieldInfo as obtained by reflection representing the
|
---|
70 | /// described type.
|
---|
71 | /// </summary>
|
---|
72 | public FieldInfo Handle {
|
---|
73 | get {
|
---|
74 | if (handle == null)
|
---|
75 | handle = DeclaringType.Handle.GetField(RealName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
---|
76 | return handle;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | #endregion
|
---|
80 |
|
---|
81 | [StorableConstructor]
|
---|
82 | private FieldDescriptor(bool deserializing) { }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Create a new field descriptor.
|
---|
86 | /// </summary>
|
---|
87 | /// <param name="declaringType">The type declaring this field.</param>
|
---|
88 | /// <param name="fieldInfo">The field info obtained by reflection.</param>
|
---|
89 | /// <param name="attribute">The storable attribute (if any) of this field.</param>
|
---|
90 | public FieldDescriptor(TypeDescriptor declaringType, FieldInfo fieldInfo, StorableAttribute attribute) {
|
---|
91 | DeclaringType = declaringType;
|
---|
92 | RealName = fieldInfo.Name;
|
---|
93 | if (attribute != null) {
|
---|
94 | StoredName = attribute.Name;
|
---|
95 | DefaultValue = attribute.DefaultValue;
|
---|
96 | }
|
---|
97 | handle = fieldInfo;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /// <summary>
|
---|
101 | /// Create a string representation of this field descriptor.
|
---|
102 | /// </summary>
|
---|
103 | public override string ToString() {
|
---|
104 | StringBuilder sb = new StringBuilder();
|
---|
105 | sb.Append(RealName);
|
---|
106 | if (StoredName != null)
|
---|
107 | sb.AppendFormat(" ({0})", StoredName);
|
---|
108 | if (DefaultValue != null)
|
---|
109 | sb.AppendFormat(" [{0}]", DefaultValue);
|
---|
110 | return sb.ToString();
|
---|
111 | }
|
---|
112 |
|
---|
113 | /// <summary>
|
---|
114 | /// Creates a shallow copy of this field descriptor.
|
---|
115 | /// </summary>
|
---|
116 | public object Clone() {
|
---|
117 | var clone = new FieldDescriptor(false);
|
---|
118 | clone.DeclaringType = DeclaringType;
|
---|
119 | clone.RealName = RealName;
|
---|
120 | clone.StoredName = StoredName;
|
---|
121 | clone.DefaultValue = DefaultValue;
|
---|
122 | clone.handle = handle;
|
---|
123 | return clone;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /// <summary>
|
---|
127 | /// From the given object return the value of the
|
---|
128 | /// field described by this field descriptor.
|
---|
129 | /// </summary>
|
---|
130 | public object GetValue(object o) {
|
---|
131 | return Handle.GetValue(o);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /// <summary>
|
---|
135 | /// From the given object set the value of the
|
---|
136 | /// field described by this field descriptor.
|
---|
137 | /// </summary>
|
---|
138 | public void SetValue(object o, object value) {
|
---|
139 | Handle.SetValue(o, value);
|
---|
140 | }
|
---|
141 |
|
---|
142 | }
|
---|
143 |
|
---|
144 | }
|
---|