public static TAttribute[] GetAttributes <TAttribute>([NotNull] Type classType) where TAttribute : Attribute { List <TAttribute> list = null; object[] attributes; if (AttributesByClass.TryGetValue(classType, out attributes)) { for (int n = attributes.Length - 1; n >= 0; n--) { var cast = attributes[n] as TAttribute; if (cast != null) { if (list == null) { list = new List <TAttribute>(0); } list.Add(cast); } } } else { #if CSHARP_7_3_OR_NEWER foreach (var attributeType in PluginAttributeConverterProvider.GetAttributeTypeAndEachAlias(typeof(TAttribute))) { var attribute = classType.GetCustomAttribute(attributeType, InheritMemberAttributes); if (attribute != null) { TAttribute cast; if (PluginAttributeConverterProvider.TryConvert(attribute, out cast)) { if (list == null) { list = new List <TAttribute>(0); } list.Add(cast); } } } #else attributes = classType.GetCustomAttributes(typeof(TAttribute), InheritClassAttributes); PluginAttributeConverterProvider.ConvertAll(ref attributes); for (int n = attributes.Length - 1; n >= 0; n--) { var cast = attributes[n] as TAttribute; if (cast != null) { if (list == null) { list = new List <TAttribute>(0); } list.Add(cast); } } #endif } return(list == null ? ArrayPool <TAttribute> .ZeroSizeArray : list.ToArray()); }
private static void AddAliases([NotNull] Type classType, [NotNull] ref IEnumerable <TAttribute> attributes) { var aliasAttributeTypes = PluginAttributeConverterProvider.GetAliases(typeof(TAttribute)); for (int n = aliasAttributeTypes.Length - 1; n >= 0; n--) { var aliasAttributeInstances = classType.GetCustomAttributes(aliasAttributeTypes[n], false); for (int a = aliasAttributeInstances.Length - 1; a >= 0; a--) { TAttribute converted; if (PluginAttributeConverterProvider.TryConvert(aliasAttributeInstances[a], out converted)) { #if !NET_2_0 && !NET_2_0_SUBSET && !NET_STANDARD_2_0 attributes = attributes.Append(converted); #else attributes = Append(attributes, converted); #endif } } } }
public static TAttribute GetAttribute <TAttribute>([NotNull] MemberInfo member) where TAttribute : Attribute { object[] attributes; if (AttributesByMember.TryGetValue(member, out attributes)) { for (int n = attributes.Length - 1; n >= 0; n--) { var cast = attributes[n] as TAttribute; if (cast != null) { return(cast); } } return(null); } #if CSHARP_7_3_OR_NEWER foreach (var attributeType in PluginAttributeConverterProvider.GetAttributeTypeAndEachAlias(typeof(TAttribute))) { var attribute = member.GetCustomAttribute(attributeType, InheritMemberAttributes); if (attribute != null) { TAttribute result; if (PluginAttributeConverterProvider.TryConvert(attribute, out result)) { return(result); } } } #else attributes = member.GetCustomAttributes(typeof(TAttribute), InheritMemberAttributes); if (attributes.Length > 0) { return(attributes[0] as TAttribute); } #endif return(null); }