<PackageReference Include="Namotion.Reflection" Version="3.4.3" />

AttributeExtensions

public static class AttributeExtensions
Provides attribute extensions.
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; namespace Namotion.Reflection { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class AttributeExtensions { [return: System.Runtime.CompilerServices.Nullable(2)] public static T GetContextOrTypeAttribute<[System.Runtime.CompilerServices.Nullable(0)] T>(this ContextualType contextualType, bool inherit) where T : Attribute { object[] customAttributes = contextualType.Context.GetCustomAttributes(typeof(T), inherit); if (customAttributes.Length == 1) return (T)customAttributes[0]; customAttributes = contextualType.GetCustomAttributes(typeof(T), inherit); if (customAttributes.Length == 1) return (T)customAttributes[0]; return null; } public static IEnumerable<T> GetContextOrTypeAttributes<[System.Runtime.CompilerServices.Nullable(0)] T>(this ContextualType contextualType, bool inherit) where T : Attribute { return contextualType.Context.GetCustomAttributes(typeof(T), inherit).Concat(contextualType.GetCustomAttributes(typeof(T), inherit)).OfType<T>(); } public static IEnumerable<Attribute> GetContextOrTypeAttributes(this ContextualType contextualType, bool inherit) { return contextualType.GetContextOrTypeAttributes<Attribute>(inherit); } [return: System.Runtime.CompilerServices.Nullable(2)] public static T GetContextAttribute<[System.Runtime.CompilerServices.Nullable(0)] T>(this ContextualType contextualType, bool inherit) where T : Attribute { object[] customAttributes = contextualType.Context.GetCustomAttributes(typeof(T), inherit); if (customAttributes.Length == 1) return (T)customAttributes[0]; return null; } public static IEnumerable<T> GetContextAttributes<[System.Runtime.CompilerServices.Nullable(0)] T>(this ContextualType contextualType, bool inherit) where T : Attribute { return contextualType.Context.GetCustomAttributes(typeof(T), inherit).OfType<T>(); } public static IEnumerable<Attribute> GetContextAttributes(this ContextualType contextualType, bool inherit) { return contextualType.GetContextAttributes<Attribute>(inherit); } public static bool IsContextAttributeDefined<[System.Runtime.CompilerServices.Nullable(0)] T>(this ContextualType contextualType, bool inherit) where T : Attribute { return contextualType.Context.IsDefined(typeof(T), inherit); } [return: System.Runtime.CompilerServices.Nullable(2)] public static T GetAttribute<[System.Runtime.CompilerServices.Nullable(0)] T>(this ContextualMemberInfo info, bool inherit) where T : Attribute { return ((ICustomAttributeProvider)info).GetAttribute<T>(inherit); } public static IEnumerable<T> GetAttributes<[System.Runtime.CompilerServices.Nullable(2)] T>(this ContextualMemberInfo info, bool inherit) { return ((ICustomAttributeProvider)info).GetAttributes<T>(inherit); } public static IEnumerable<Attribute> GetAttributes(this ContextualMemberInfo info, bool inherit) { return ((ICustomAttributeProvider)info).GetAttributes(inherit); } public static bool IsAttributeDefined<[System.Runtime.CompilerServices.Nullable(0)] T>(this ContextualMemberInfo info, bool inherit) where T : Attribute { return ((ICustomAttributeProvider)info).IsAttributeDefined<T>(inherit); } [return: System.Runtime.CompilerServices.Nullable(2)] public static T GetAttribute<[System.Runtime.CompilerServices.Nullable(0)] T>(this ContextualParameterInfo info, bool inherit) where T : Attribute { return ((ICustomAttributeProvider)info).GetAttribute<T>(inherit); } public static IEnumerable<T> GetAttributes<[System.Runtime.CompilerServices.Nullable(2)] T>(this ContextualParameterInfo info, bool inherit) { return ((ICustomAttributeProvider)info).GetAttributes<T>(inherit); } public static IEnumerable<Attribute> GetAttributes(this ContextualParameterInfo info, bool inherit) { return ((ICustomAttributeProvider)info).GetAttributes(inherit); } public static bool IsAttributeDefined<[System.Runtime.CompilerServices.Nullable(0)] T>(this ContextualParameterInfo info, bool inherit) where T : Attribute { return ((ICustomAttributeProvider)info).IsAttributeDefined<T>(inherit); } [return: System.Runtime.CompilerServices.Nullable(2)] public static T GetAttribute<[System.Runtime.CompilerServices.Nullable(0)] T>(this CachedType info, bool inherit) where T : Attribute { return ((ICustomAttributeProvider)info).GetAttribute<T>(inherit); } public static IEnumerable<T> GetAttributes<[System.Runtime.CompilerServices.Nullable(2)] T>(this CachedType info, bool inherit) { return ((ICustomAttributeProvider)info).GetAttributes<T>(inherit); } public static IEnumerable<Attribute> GetAttributes(this CachedType info, bool inherit) { return ((ICustomAttributeProvider)info).GetAttributes(inherit); } public static bool IsAttributeDefined<[System.Runtime.CompilerServices.Nullable(0)] T>(this CachedType info, bool inherit) where T : Attribute { return ((ICustomAttributeProvider)info).IsAttributeDefined<T>(inherit); } [return: System.Runtime.CompilerServices.Nullable(2)] private static T GetAttribute<[System.Runtime.CompilerServices.Nullable(0)] T>(this ICustomAttributeProvider provider, bool inherit) where T : Attribute { object[] customAttributes = provider.GetCustomAttributes(typeof(T), inherit); if (customAttributes.Length == 1) return (T)customAttributes[0]; return null; } private static IEnumerable<T> GetAttributes<[System.Runtime.CompilerServices.Nullable(2)] T>(this ICustomAttributeProvider provider, bool inherit) { return provider.GetCustomAttributes(typeof(T), inherit).OfType<T>(); } private static IEnumerable<Attribute> GetAttributes(this ICustomAttributeProvider provider, bool inherit) { return provider.GetAttributes<Attribute>(inherit); } private static bool IsAttributeDefined<[System.Runtime.CompilerServices.Nullable(0)] T>(this ICustomAttributeProvider provider, bool inherit) where T : Attribute { return provider.IsDefined(typeof(T), inherit); } } }