CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
Csla/PropertyInfo.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="PropertyInfo.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Maintains metadata about a property.</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Linq;
10using System.ComponentModel;
11using System.ComponentModel.DataAnnotations;
12
13namespace Csla
14{
21 public class PropertyInfo<T> : Core.IPropertyInfo, IComparable
22 {
27 public PropertyInfo(string name)
28 : this(name, null, null, DataBindingFriendlyDefault(), RelationshipTypes.None)
29 { }
30
36 public PropertyInfo(string name, RelationshipTypes relationship)
37 : this(name, null, null, DataBindingFriendlyDefault(), relationship)
38 { }
39
47 public PropertyInfo(string name, T defaultValue)
48 : this(name, null, null, defaultValue, RelationshipTypes.None)
49 { }
50
58 public PropertyInfo(string name, string friendlyName)
59 : this(name, friendlyName, null, DataBindingFriendlyDefault(), RelationshipTypes.None)
60 { }
61
72 public PropertyInfo(string name, string friendlyName, Type containingType)
73 : this(name, friendlyName, containingType, DataBindingFriendlyDefault(), RelationshipTypes.None)
74 { }
75
89 public PropertyInfo(string name, string friendlyName, Type containingType, T defaultValue)
90 : this(name, friendlyName, containingType, defaultValue, RelationshipTypes.None)
91 { }
92
104 public PropertyInfo(string name, string friendlyName, Type containingType, RelationshipTypes relationship)
105 : this(name, friendlyName, null, DataBindingFriendlyDefault(), relationship)
106 { }
107
123 public PropertyInfo(string name, string friendlyName, Type containingType, T defaultValue, RelationshipTypes relationship)
124 {
125 Name = name;
126 _friendlyName = friendlyName;
127 _relationshipType = relationship;
128 if (containingType != null)
129 _propertyInfo = containingType.GetProperty(Name);
130
131 _defaultValue = defaultValue;
132 }
133
137 public string Name { get; private set; }
138
142 public Type Type
143 {
144 get { return typeof(T); }
145 }
146
147 private readonly System.Reflection.PropertyInfo _propertyInfo;
148
149 private readonly string _friendlyName;
159 public virtual string FriendlyName
160 {
161 get
162 {
163 string result = Name;
164 if (!string.IsNullOrWhiteSpace(_friendlyName))
165 {
166 result = _friendlyName;
167 }
168 else if (_propertyInfo != null)
169 {
170 var display = _propertyInfo.GetCustomAttributes(typeof(DisplayAttribute), true).OfType<DisplayAttribute>().FirstOrDefault();
171 if (display != null)
172 {
173 // DataAnnotations attribute.
174 result = display.GetName();
175 }
176 else
177 {
178 // ComponentModel attribute.
179 var displayName = _propertyInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true).OfType<DisplayNameAttribute>().FirstOrDefault();
180 if (displayName != null)
181 result = displayName.DisplayName;
182 }
183 }
184 return result;
185 }
186 }
187
188 private readonly T _defaultValue;
198 public virtual T DefaultValue
199 {
200 get { return _defaultValue; }
201 }
202
203 object Core.IPropertyInfo.DefaultValue
204 {
205 get { return DefaultValue; }
206 }
207
208 Core.FieldManager.IFieldData Core.IPropertyInfo.NewFieldData(string name)
209 {
210 return NewFieldData(name);
211 }
212
221 protected virtual Core.FieldManager.IFieldData NewFieldData(string name)
222 {
223 return new Core.FieldManager.FieldData<T>(name);
224 }
225
226
227 // Default value is ManagedField
228 private RelationshipTypes _relationshipType = RelationshipTypes.None;
229
235 {
236 get { return _relationshipType; }
237 }
238
239 private int _index = -1;
240
246 int Core.IPropertyInfo.Index
247 {
248 get { return _index; }
249 set { _index = value; }
250 }
251
256 public bool IsChild { get => typeof(Core.IBusinessObject).IsAssignableFrom(Type); }
257
263 public System.Reflection.PropertyInfo GetPropertyInfo() => _propertyInfo;
264
265 #region IComparable Members
266
267 int IComparable.CompareTo(object obj)
268 {
269 return Name.CompareTo(((Core.IPropertyInfo)obj).Name);
270 }
271
272 #endregion
273
279 {
280 // if T is string we need an empty string, not null, for data binding
281 if (typeof(T) == typeof(string))
282 return (T)(object)string.Empty;
283
284 return default(T);
285 }
286 }
287}
Maintains metadata about a property.
virtual T DefaultValue
Gets the default initial value for the property.
PropertyInfo(string name)
Creates a new instance of this class.
RelationshipTypes RelationshipType
Gets the relationship between the declaring object and the object reference in the property.
PropertyInfo(string name, string friendlyName, Type containingType, T defaultValue)
Creates a new instance of this class.
PropertyInfo(string name, T defaultValue)
Creates a new instance of this class.
Type Type
Gets the type of the property.
static T DataBindingFriendlyDefault()
Creates the CSLA Data Binding Friendly default for the given type T.
PropertyInfo(string name, string friendlyName, Type containingType, T defaultValue, RelationshipTypes relationship)
Creates a new instance of this class.
System.Reflection.PropertyInfo GetPropertyInfo()
Gets the System.Reflection.PropertyInfo object representing the property.
string Name
Gets the property name value.
PropertyInfo(string name, RelationshipTypes relationship)
Creates a new instance of this class.
bool IsChild
Gets a value indicating whether this property references a child in the object graph.
virtual string FriendlyName
Gets the friendly display name for the property.
PropertyInfo(string name, string friendlyName, Type containingType)
Creates a new instance of this class.
virtual Core.FieldManager.IFieldData NewFieldData(string name)
Create and return a new IFieldData object to store an instance value for this property.
PropertyInfo(string name, string friendlyName, Type containingType, RelationshipTypes relationship)
Creates a new instance of this class.
PropertyInfo(string name, string friendlyName)
Creates a new instance of this class.
RelationshipTypes
List of valid relationship types between a parent object and another object through a managed propert...