-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReflectionExtensions.cs
More file actions
172 lines (159 loc) · 6.94 KB
/
ReflectionExtensions.cs
File metadata and controls
172 lines (159 loc) · 6.94 KB
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace Extensions
{
public static class ReflectionExtensions
{
/// <summary>
/// Gets the specified attributeToFind from the source property
/// </summary>
/// <typeparam name="T"> Type of items to use. </typeparam>
/// <param name="property"> The property info who's attributeToFind we are fetching. </param>
/// <param name="inherit"> Specifies whether to search this member's inheritance chain to find the attributes. </param>
/// <returns> The attributes of supplied property info. </returns>
public static T GetAttribute<T>(this PropertyInfo property, bool inherit = true) where T : Attribute
{
if (property.IsNotNull())
{
return
property.GetCustomAttributes(typeof (T), inherit).ValidateNotEmpty("attributeToFind").First().As<T>();
}
return null;
}
/// <summary>
/// Get the DisplayName of the provided property info item if it has one else return the property name
/// </summary>
/// <param name="type"> Type of items to use. </param>
/// <returns> The DisplayName attributeToFind of type or TyneName if no display name. </returns>
public static string GetName(this Type type)
{
var name = type.Name;
var displayName = type.GetCustomAttributes(typeof (DisplayNameAttribute), true);
if (displayName.Any())
name = ((DisplayNameAttribute) displayName.First()).DisplayName;
return name;
}
/// <summary>
/// Get the DisplayName of the provided property info item if it has one else return the property name
/// </summary>
/// <returns> The DisplayName attributeToFind of property or property name if no display name. </returns>
public static string GetName(this PropertyInfo pi)
{
var name = pi.Name;
var displayName = pi.GetCustomAttributes(typeof (DisplayNameAttribute), true);
if (displayName.Any())
name = ((DisplayNameAttribute) displayName.First()).DisplayName;
return name;
}
/// <summary>
/// Get the properties of a type which have an attributeToFind
/// </summary>
/// <param name="t"> A class type. </param>
/// <returns> List of properties </returns>
public static List<PropertyInfo> GetPropertyWithAttribute<T>(this Type t) where T : Attribute
{
return t.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.Where(p => p.GetCustomAttributes(typeof (T), false).Count() == 1).ToList();
}
/// <summary>
/// Gets the specified property value from the source object
/// </summary>
/// <param name="instance"> Object which we want to get property value for. </param>
/// <param name="propertyName"> The name of the property we want to get. </param>
/// <returns> </returns>
public static object GetValue(this object instance, string propertyName)
{
if (instance.IsNotNull())
{
return instance
.GetType()
.GetProperty(propertyName)
.ValidateNotNull("property")
.GetValue(instance, null);
}
return null;
}
/// <summary>
/// Gets the specified property value from the source object as the specified type
/// </summary>
/// <typeparam name="T"> Type of items to use. </typeparam>
/// <param name="instance"> Object which we want to get property value for. </param>
/// <param name="propertyName"> The name of the property we want to get. </param>
/// <returns> </returns>
public static T GetValue<T>(this object instance, string propertyName)
{
if (instance.IsNotNull())
{
return instance
.GetType()
.GetProperty(propertyName)
.ValidateNotNull("property")
.GetValue(instance, null)
.As<T>();
}
return default(T);
}
/// <summary>
/// Verifies that the source property has the specified generic attributeToFind
/// </summary>
/// <typeparam name="T"> Type of items to use. </typeparam>
/// <param name="property"> The property to check. </param>
/// <param name="inhert"> Specifies whether to search this member's inheritance chain to find the attributes. </param>
/// <returns> </returns>
public static bool HasAttribute<T>(this PropertyInfo property, bool inhert = true) where T : Attribute
{
return (property.GetCustomAttributes(typeof (T), true).Length > 0);
}
/// <summary>
/// Checks if type is nullable
/// </summary>
/// <param name="type"> Type to check </param>
/// <returns> </returns>
public static bool IsNullableType(this Type type)
{
return type.IsGenericType
&& type.GetGenericTypeDefinition() == typeof (Nullable<>);
}
/// <summary>
/// Checks if property is nullable
/// </summary>
/// <param name="propertyInfo"> Property to check </param>
/// <returns> </returns>
public static bool IsNullableType(this PropertyInfo propertyInfo)
{
return propertyInfo.PropertyType.IsGenericType
&& propertyInfo.PropertyType.GetGenericTypeDefinition() == typeof (Nullable<>);
}
/// <summary>
/// Determines whether objects of the supplied type are numeric.
/// </summary>
/// <param name="type"> Type to check. </param>
/// <returns> True if objects of the supplied type are numeric. </returns>
public static bool IsNumeric(this Type type)
{
return TypeHelper.NumericTypes().ContainsKey(type.Name);
}
/// <summary>
/// Sets the specified property to the specified value on the source object
/// </summary>
/// <param name="instance"> Object whose property will be set. </param>
/// <param name="propertyName"> Property name to set. </param>
/// <param name="value"> Property value to set. </param>
/// <returns> </returns>
public static object SetProperty(this object instance, string propertyName, object value)
{
if (instance.IsNotNull())
{
instance
.GetType()
.GetProperty(propertyName)
.ValidateNotNull("property")
.SetValue(instance, value, null);
}
return instance;
}
}
}