forked from zhaoqingqing/blog_samplecode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomAttributeHelper.cs
109 lines (96 loc) · 3.47 KB
/
CustomAttributeHelper.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
using System;
using System.Collections.Generic;
using System.Linq;
public static class CustomAttributeHelper
{
/// <summary>
/// Cache Data
/// </summary>
private static readonly Dictionary<string, string> Cache = new Dictionary<string, string>();
/// <summary>
/// 获取CustomAttribute Value
/// </summary>
/// <typeparam name="T">Attribute的子类型</typeparam>
/// <param name="sourceType">头部标有CustomAttribute类的类型</param>
/// <param name="attributeValueAction">取Attribute具体哪个属性值的匿名函数</param>
/// <returns>返回Attribute的值,没有则返回null</returns>
public static string GetCustomAttributeValue<T>(this Type sourceType, Func<T, string> attributeValueAction) where T : Attribute
{
return GetAttributeValue(sourceType, attributeValueAction, null);
}
/// <summary>
/// 获取CustomAttribute Value
/// </summary>
/// <typeparam name="T">Attribute的子类型</typeparam>
/// <param name="sourceType">头部标有CustomAttribute类的类型</param>
/// <param name="attributeValueAction">取Attribute具体哪个属性值的匿名函数</param>
/// <param name="name">field name或property name</param>
/// <returns>返回Attribute的值,没有则返回null</returns>
public static string GetCustomAttributeValue<T>(this Type sourceType, Func<T, string> attributeValueAction,
string name) where T : Attribute
{
return GetAttributeValue(sourceType, attributeValueAction, name);
}
private static string GetAttributeValue<T>(Type sourceType, Func<T, string> attributeValueAction,
string name) where T : Attribute
{
var key = BuildKey(sourceType, name);
if (!Cache.ContainsKey(key))
{
CacheAttributeValue(sourceType, attributeValueAction, name);
}
return Cache[key];
}
/// <summary>
/// 缓存Attribute Value
/// </summary>
private static void CacheAttributeValue<T>(Type type,
Func<T, string> attributeValueAction, string name)
{
var key = BuildKey(type, name);
var value = GetValue(type, attributeValueAction, name);
lock (key + "_attributeValueLockKey")
{
if (!Cache.ContainsKey(key))
{
Cache[key] = value;
}
}
}
private static string GetValue<T>(Type type,
Func<T, string> attributeValueAction, string name)
{
object attribute = null;
if (string.IsNullOrEmpty(name))
{
attribute =
type.GetCustomAttributes(typeof(T), false).FirstOrDefault();
}
else
{
var propertyInfo = type.GetProperty(name);
if (propertyInfo != null)
{
attribute =
propertyInfo.GetCustomAttributes(typeof(T), false).FirstOrDefault();
}
var fieldInfo = type.GetField(name);
if (fieldInfo != null)
{
attribute = fieldInfo.GetCustomAttributes(typeof(T), false).FirstOrDefault();
}
}
return attribute == null ? null : attributeValueAction((T)attribute);
}
/// <summary>
/// 缓存Collection Name Key
/// </summary>
private static string BuildKey(Type type, string name)
{
if (string.IsNullOrEmpty(name))
{
return type.FullName;
}
return type.FullName + "." + name;
}
}