-
-
Notifications
You must be signed in to change notification settings - Fork 389
/
PropertyInfo.cs
287 lines (258 loc) · 8.9 KB
/
PropertyInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//-----------------------------------------------------------------------
// <copyright file="PropertyInfo.cs" company="Marimer LLC">
// Copyright (c) Marimer LLC. All rights reserved.
// Website: https://cslanet.com
// </copyright>
// <summary>Maintains metadata about a property.</summary>
//-----------------------------------------------------------------------
using System;
using System.Linq;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Csla
{
/// <summary>
/// Maintains metadata about a property.
/// </summary>
/// <typeparam name="T">
/// Data type of the property.
/// </typeparam>
public class PropertyInfo<T> : Core.IPropertyInfo, IComparable
{
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="name">Name of the property.</param>
public PropertyInfo(string name)
: this(name, null, null, DataBindingFriendlyDefault(), RelationshipTypes.None)
{ }
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <param name="relationship">Relationship with referenced object.</param>
public PropertyInfo(string name, RelationshipTypes relationship)
: this(name, null, null, DataBindingFriendlyDefault(), relationship)
{ }
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <param name="defaultValue">
/// Default value for the property.
/// </param>
public PropertyInfo(string name, T defaultValue)
: this(name, null, null, defaultValue, RelationshipTypes.None)
{ }
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <param name="friendlyName">
/// Friendly display name for the property.
/// </param>
public PropertyInfo(string name, string friendlyName)
: this(name, friendlyName, null, DataBindingFriendlyDefault(), RelationshipTypes.None)
{ }
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <param name="friendlyName">
/// Friendly display name for the property.
/// </param>
/// <param name="containingType">
/// Factory to provide display name from attributes.
/// </param>
public PropertyInfo(string name, string friendlyName, Type containingType)
: this(name, friendlyName, containingType, DataBindingFriendlyDefault(), RelationshipTypes.None)
{ }
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <param name="friendlyName">
/// Friendly display name for the property.
/// </param>
/// <param name="containingType">
/// Factory to provide display name from attributes.
/// </param>
/// <param name="defaultValue">
/// Default value for the property.
/// </param>
public PropertyInfo(string name, string friendlyName, Type containingType, T defaultValue)
: this(name, friendlyName, containingType, defaultValue, RelationshipTypes.None)
{ }
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <param name="friendlyName">
/// Friendly display name for the property.
/// </param>
/// <param name="containingType">
/// Factory to provide display name from attributes.
/// </param>
/// <param name="relationship">Relationship with referenced object.</param>
public PropertyInfo(string name, string friendlyName, Type containingType, RelationshipTypes relationship)
: this(name, friendlyName, null, DataBindingFriendlyDefault(), relationship)
{ }
/// <summary>
/// Creates a new instance of this class.
/// </summary>
/// <param name="name">Name of the property.</param>
/// <param name="friendlyName">
/// Friendly display name for the property.
/// </param>
/// <param name="containingType">
/// Factory to provide display name from attributes.
/// </param>
/// <param name="defaultValue">
/// Default value for the property.
/// </param>
/// <param name="relationship">Relationship with
/// referenced object.</param>
public PropertyInfo(string name, string friendlyName, Type containingType, T defaultValue, RelationshipTypes relationship)
{
Name = name;
_friendlyName = friendlyName;
_relationshipType = relationship;
if (containingType != null)
_propertyInfo = containingType.GetProperty(Name);
_defaultValue = defaultValue;
}
/// <summary>
/// Gets the property name value.
/// </summary>
public string Name { get; private set; }
/// <summary>
/// Gets the type of the property.
/// </summary>
public Type Type
{
get { return typeof(T); }
}
private readonly System.Reflection.PropertyInfo _propertyInfo;
private readonly string _friendlyName;
/// <summary>
/// Gets the friendly display name
/// for the property.
/// </summary>
/// <remarks>
/// If no friendly name was provided, the
/// property name itself is returned as a
/// result.
/// </remarks>
public virtual string FriendlyName
{
get
{
string result = Name;
if (!string.IsNullOrWhiteSpace(_friendlyName))
{
result = _friendlyName;
}
else if (_propertyInfo != null)
{
var display = _propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true).OfType<DisplayAttribute>().FirstOrDefault();
if (display != null)
{
// DataAnnotations attribute.
result = display.GetName();
}
else
{
// ComponentModel attribute.
var displayName = _propertyInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true).OfType<DisplayNameAttribute>().FirstOrDefault();
if (displayName != null)
result = displayName.DisplayName;
}
}
return result;
}
}
private readonly T _defaultValue;
/// <summary>
/// Gets the default initial value for the property.
/// </summary>
/// <remarks>
/// This value is used to initialize the property's
/// value, and is returned from a property get
/// if the user is not authorized to
/// read the property.
/// </remarks>
public virtual T DefaultValue
{
get { return _defaultValue; }
}
object Core.IPropertyInfo.DefaultValue
{
get { return DefaultValue; }
}
Core.FieldManager.IFieldData Core.IPropertyInfo.NewFieldData(string name)
{
return NewFieldData(name);
}
/// <summary>
/// Create and return a new IFieldData object
/// to store an instance value for this
/// property.
/// </summary>
/// <param name="name">
/// Property name.
/// </param>
protected virtual Core.FieldManager.IFieldData NewFieldData(string name)
{
return new Core.FieldManager.FieldData<T>(name);
}
// Default value is ManagedField
private RelationshipTypes _relationshipType = RelationshipTypes.None;
/// <summary>
/// Gets the relationship between the declaring object
/// and the object reference in the property.
/// </summary>
public RelationshipTypes RelationshipType
{
get { return _relationshipType; }
}
private int _index = -1;
/// <summary>
/// Gets or sets the index position for the managed
/// field storage behind the property. FOR
/// INTERNAL CSLA .NET USE ONLY.
/// </summary>
int Core.IPropertyInfo.Index
{
get { return _index; }
set { _index = value; }
}
/// <summary>
/// Gets a value indicating whether this property
/// references a child in the object graph.
/// </summary>
public bool IsChild { get => typeof(Core.IBusinessObject).IsAssignableFrom(Type); }
/// <summary>
/// Gets the System.Reflection.PropertyInfo object
/// representing the property.
/// </summary>
/// <returns></returns>
public System.Reflection.PropertyInfo GetPropertyInfo() => _propertyInfo;
#region IComparable Members
int IComparable.CompareTo(object obj)
{
return Name.CompareTo(((Core.IPropertyInfo)obj).Name);
}
#endregion
/// <summary>
/// Creates the CSLA Data Binding Friendly default for the given type T.
/// </summary>
/// <returns>Default value for T which is compatible with Data Binding</returns>
public static T DataBindingFriendlyDefault()
{
// if T is string we need an empty string, not null, for data binding
if (typeof(T) == typeof(string))
return (T)(object)string.Empty;
return default(T);
}
}
}