???????????????Address?????????Attribute??????????????????????????????????????????????????????????????????????????Attribute??????????????μ??????????????Attribute??????????д??δ??????????????????Щ???????Υ????DRY??????????????????Attribute???????????????Attribute???????????????????????ζ?????з??????????????????????????????????μ??????????????????£?
1     public static class CustomAttributeHelper
2     {
3         /// <summary>
4         /// Cache Data
5         /// </summary>
6         private static readonly Dictionary<string?? string> Cache = new Dictionary<string?? string>();
7
8         /// <summary>
9         /// ???CustomAttribute Value
10         /// </summary>
11         /// <typeparam name="T">Attribute????????</typeparam>
12         /// <param name="sourceType">???????CustomAttribute???????</param>
13         /// <param name="attributeValueAction">?Attribute??????????????????????</param>
14         /// <returns>????Attribute????????????null</returns>
15         public static string GetCustomAttributeValue<T>(this Type sourceType?? Func<T?? string> attributeValueAction) where T : Attribute
16         {
17             return GetAttributeValue(sourceType?? attributeValueAction?? null);
18         }
19
20         /// <summary>
21         /// ???CustomAttribute Value
22         /// </summary>
23         /// <typeparam name="T">Attribute????????</typeparam>
24         /// <param name="sourceType">???????CustomAttribute???????</param>
25         /// <param name="attributeValueAction">?Attribute??????????????????????</param>
26         /// <param name="name">field name??property name</param>
27         /// <returns>????Attribute????????????null</returns>
28         public static string GetCustomAttributeValue<T>(this Type sourceType?? Func<T?? string> attributeValueAction??
29             string name) where T : Attribute
30         {
31             return GetAttributeValue(sourceType?? attributeValueAction?? name);
32         }
33
34         private static string GetAttributeValue<T>(Type sourceType?? Func<T?? string> attributeValueAction??
35             string name) where T : Attribute
36         {
37             var key = BuildKey(sourceType?? name);
38             if (!Cache.ContainsKey(key))
39             {
40                 CacheAttributeValue(sourceType?? attributeValueAction?? name);
41             }
42
43             return Cache[key];
44         }
45
46         /// <summary>
47         /// ????Attribute Value
48         /// </summary>
49         private static void CacheAttributeValue<T>(Type type??
50             Func<T?? string> attributeValueAction?? string name)
51         {
52             var key = BuildKey(type?? name);
53
54             var value = GetValue(type?? attributeValueAction?? name);
55
56             lock (key + "_attributeValueLockKey")
57             {
58                 if (!Cache.ContainsKey(key))
59                 {
60                     Cache[key] = value;
61                 }
62             }
63         }
64
65         private static string GetValue<T>(Type type??
66             Func<T?? string> attributeValueAction?? string name)
67         {
68             object attribute = null;
69             if (string.IsNullOrEmpty(name))
70             {
71                 attribute =
72                     type.GetCustomAttributes(typeof (T)?? false).FirstOrDefault();
73             }
74             else
75             {
76                 var propertyInfo = type.GetProperty(name);
77                 if (propertyInfo != null)
78                 {
79                     attribute =
80                         propertyInfo.GetCustomAttributes(typeof (T)?? false).FirstOrDefault();
81                 }
82
83                 var fieldInfo = type.GetField(name);
84                 if (fieldInfo != null)
85                 {
86                     attribute = fieldInfo.GetCustomAttributes(typeof (T)?? false).FirstOrDefault();
87                 }
88             }
89
90             return attribute == null ? null : attributeValueAction((T) attribute);
91         }
92
93         /// <summary>
94         /// ????Collection Name Key
95         /// </summary>
96         private static string BuildKey(Type type?? string name)
97         {
98             if (string.IsNullOrEmpty(name))
99             {
100                 return type.FullName;
101             }
102
103             return type.FullName + "." + name;
104         }
105     }
?????????????????
??????????????÷???T??Fun<T??stirng>????????????????????
???????????Attribute??浽???Dictionary?У??′??????????????????????Dictionary?е?????????в????????????????Attribute??????????????Ч???
???????÷????????????????????£?
????1             var cName=typeof(CustomAttributes).GetCustomAttributeValue<NameAttribute>(x => x.Name);
????2             var fName = typeof (CustomAttributes).GetCustomAttributeValue<NameAttribute>(x => x.Name?? "Address");
????????У? ???????????????÷??????????????????