/// <summary> /// 获取特性 /// </summary> /// <typeparam name="T">Attribute类型</typeparam> /// <param name="element">MemberInfo</param> /// <returns></returns> private static T GetCustomAttribute <T>(MemberInfo element) where T : Attribute { T result; Type attrType = typeof(T); AttrDictEntry key = CalculateKey(element, attrType, true); result = (T)attributeDictionary.GetOrAdd(key, (t) => (T)Attribute.GetCustomAttribute(element, attrType)); return(result); }
public static T GetCustomAttribute <T>(MemberInfo element) where T : Attribute { T result = default(T); System.Type attrType = typeof(T); AttrDictEntry key = CalculateKey(element, attrType, true); lock (AttributeHelper.dictionary) { if (AttributeHelper.dictionary.ContainsKey(key)) { result = (T)AttributeHelper.dictionary[key]; } else { result = (T)Attribute.GetCustomAttribute(element, attrType); AttributeHelper.dictionary[key] = result; } } return(result); }
public static T GetCustomAttribute <T>(MemberInfo element) where T : Attribute { T result = default(T); System.Type attrType = typeof(T); AttrDictEntry key = CalculateKey(element, attrType, true); lock (AttributeHelper.dictionary) { Attribute data = null; if (AttributeHelper.dictionary.TryGetValue(key, out data) == false) { data = (T)Attribute.GetCustomAttribute(element, attrType); AttributeHelper.dictionary.Add(key, data); } result = (T)data; } return(result); }