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
{
[System.Runtime.CompilerServices.NullableContext(2)]
public static T GetContextOrTypeAttribute<T>([System.Runtime.CompilerServices.Nullable(1)] this ContextualType contextualType, bool inherit)
{
T val = (T)contextualType.Context.GetCustomAttributes(typeof(T), inherit).SingleOrDefault();
if (val == null)
return (T)contextualType.GetCustomAttributes(typeof(T), inherit).SingleOrDefault();
return val;
}
public static IEnumerable<T> GetContextOrTypeAttributes<[System.Runtime.CompilerServices.Nullable(2)] T>(this ContextualType contextualType, bool inherit)
{
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);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T GetContextAttribute<T>([System.Runtime.CompilerServices.Nullable(1)] this ContextualType contextualType, bool inherit)
{
return (T)contextualType.Context.GetCustomAttributes(typeof(T), inherit).SingleOrDefault();
}
public static IEnumerable<T> GetContextAttributes<[System.Runtime.CompilerServices.Nullable(2)] T>(this ContextualType contextualType, bool inherit)
{
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(2)] T>(this ContextualType contextualType, bool inherit)
{
return contextualType.Context.IsDefined(typeof(T), inherit);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T GetAttribute<T>([System.Runtime.CompilerServices.Nullable(1)] this ContextualMemberInfo info, bool inherit)
{
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(2)] T>(this ContextualMemberInfo info, bool inherit)
{
return ((ICustomAttributeProvider)info).IsAttributeDefined<T>(inherit);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T GetAttribute<T>([System.Runtime.CompilerServices.Nullable(1)] this ContextualParameterInfo info, bool inherit)
{
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(2)] T>(this ContextualParameterInfo info, bool inherit)
{
return ((ICustomAttributeProvider)info).IsAttributeDefined<T>(inherit);
}
[System.Runtime.CompilerServices.NullableContext(2)]
public static T GetAttribute<T>([System.Runtime.CompilerServices.Nullable(1)] this CachedType info, bool inherit)
{
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(2)] T>(this CachedType info, bool inherit)
{
return ((ICustomAttributeProvider)info).IsAttributeDefined<T>(inherit);
}
[System.Runtime.CompilerServices.NullableContext(2)]
internal static T GetAttribute<T>([System.Runtime.CompilerServices.Nullable(1)] this ICustomAttributeProvider provider, bool inherit)
{
return (T)provider.GetCustomAttributes(typeof(T), inherit).SingleOrDefault();
}
internal static IEnumerable<T> GetAttributes<[System.Runtime.CompilerServices.Nullable(2)] T>(this ICustomAttributeProvider provider, bool inherit)
{
return provider.GetCustomAttributes(typeof(T), inherit).OfType<T>();
}
internal static IEnumerable<Attribute> GetAttributes(this ICustomAttributeProvider provider, bool inherit)
{
return provider.GetAttributes<Attribute>(inherit);
}
internal static bool IsAttributeDefined<[System.Runtime.CompilerServices.Nullable(2)] T>(this ICustomAttributeProvider provider, bool inherit)
{
return provider.IsDefined(typeof(T), inherit);
}
}
}