1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public static class ReflectionExtensions
{
public static T GetCustomAttribute<T>(this object objectItem) where T : Attribute
{
return GetCustomAttribute<T>(objectItem, false);
}
public static T GetCustomAttribute<T>(this object objectItem, bool inherit) where T : Attribute
{
T attribute = null;
object[] attributes = objectItem.GetType().GetCustomAttributes(typeof(T), inherit);
if (attributes.Length == 1)
{
attribute = (T)attributes[0];
}
return attribute;
}
}
|