Skip to content

Instantly share code, notes, and snippets.

@JohannesEH
Created October 27, 2011 22:05
Show Gist options
  • Save JohannesEH/1321024 to your computer and use it in GitHub Desktop.
Save JohannesEH/1321024 to your computer and use it in GitHub Desktop.
Extension Types for easier (and strongly typed) Attribute checks
public static class AttributeExtensions
{
// Attribute.IsDefined shortcuts
public static bool HasAttribute<T>(this object element) where T : Attribute
{ return Attribute.IsDefined(element.GetType(), typeof(T)); }
public static bool HasAttribute<T>(this object element, bool inherit) where T : Attribute
{ return Attribute.IsDefined(element.GetType(), typeof(T), inherit); }
public static bool HasAttribute<T>(this Assembly element) where T : Attribute
{ return Attribute.IsDefined(element, typeof(T)); }
public static bool HasAttribute<T>(this Assembly element, bool inherit) where T : Attribute
{ return Attribute.IsDefined(element, typeof(T), inherit); }
public static bool HasAttribute<T>(this MemberInfo element) where T : Attribute
{ return Attribute.IsDefined(element, typeof(T)); }
public static bool HasAttribute<T>(this MemberInfo element, bool inherit) where T : Attribute
{ return Attribute.IsDefined(element, typeof(T), inherit); }
public static bool HasAttribute<T>(this Module element) where T : Attribute
{ return Attribute.IsDefined(element, typeof(T)); }
public static bool HasAttribute<T>(this Module element, bool inherit) where T : Attribute
{ return Attribute.IsDefined(element, typeof(T), inherit); }
public static bool HasAttribute<T>(this ParameterInfo element) where T : Attribute
{ return Attribute.IsDefined(element, typeof(T)); }
public static bool HasAttribute<T>(this ParameterInfo element, bool inherit) where T : Attribute
{ return Attribute.IsDefined(element, typeof(T), inherit); }
// Attribute.GetCustomAttribute shotcuts
public static T GetAttribute<T>(this object element) where T : Attribute
{ return Attribute.GetCustomAttribute(element.GetType(), typeof(T)) as T; }
public static T GetAttribute<T>(this object element, bool inherit) where T : Attribute
{ return Attribute.GetCustomAttribute(element.GetType(), typeof(T), inherit) as T; }
public static T GetAttribute<T>(this Assembly element) where T : Attribute
{ return Attribute.GetCustomAttribute(element, typeof(T)) as T; }
public static T GetAttribute<T>(this Assembly element, bool inherit) where T : Attribute
{ return Attribute.GetCustomAttribute(element, typeof(T), inherit) as T; }
public static T GetAttribute<T>(this MemberInfo element) where T : Attribute
{ return Attribute.GetCustomAttribute(element, typeof(T)) as T; }
public static T GetAttribute<T>(this MemberInfo element, bool inherit) where T : Attribute
{ return Attribute.GetCustomAttribute(element, typeof(T), inherit) as T; }
public static T GetAttribute<T>(this Module element) where T : Attribute
{ return Attribute.GetCustomAttribute(element, typeof(T)) as T; }
public static T GetAttribute<T>(this Module element, bool inherit) where T : Attribute
{ return Attribute.GetCustomAttribute(element, typeof(T), inherit) as T; }
public static T GetAttribute<T>(this ParameterInfo element) where T : Attribute
{ return Attribute.GetCustomAttribute(element, typeof(T)) as T; }
public static T GetAttribute<T>(this ParameterInfo element, bool inherit) where T : Attribute
{ return Attribute.GetCustomAttribute(element, typeof(T), inherit) as T; }
// Attribute.GetCustomAttributes shortcuts
public static T[] GetAttributes<T>(this object element) where T : Attribute
{ return Attribute.GetCustomAttributes(element.GetType(), typeof(T)) as T[]; }
public static T[] GetAttributes<T>(this object element, bool inherit) where T : Attribute
{ return Attribute.GetCustomAttributes(element.GetType(), typeof(T), inherit) as T[]; }
public static T[] GetAttributes<T>(this Assembly element) where T : Attribute
{ return Attribute.GetCustomAttributes(element, typeof(T)) as T[]; }
public static T[] GetAttributes<T>(this Assembly element, bool inherit) where T : Attribute
{ return Attribute.GetCustomAttributes(element, typeof(T), inherit) as T[]; }
public static T[] GetAttributes<T>(this MemberInfo element) where T : Attribute
{ return Attribute.GetCustomAttributes(element, typeof(T)) as T[]; }
public static T[] GetAttributes<T>(this MemberInfo element, bool inherit) where T : Attribute
{ return Attribute.GetCustomAttributes(element, typeof(T), inherit) as T[]; }
public static T[] GetAttributes<T>(this Module element) where T : Attribute
{ return Attribute.GetCustomAttributes(element, typeof(T)) as T[]; }
public static T[] GetAttributes<T>(this Module element, bool inherit) where T : Attribute
{ return Attribute.GetCustomAttributes(element, typeof(T), inherit) as T[]; }
public static T[] GetAttributes<T>(this ParameterInfo element) where T : Attribute
{ return Attribute.GetCustomAttributes(element, typeof(T)) as T[]; }
public static T[] GetAttributes<T>(this ParameterInfo element, bool inherit) where T : Attribute
{ return Attribute.GetCustomAttributes(element, typeof(T), inherit) as T[]; }
// ICustomAttributeProvider shortcuts (contributed by AutoRevit, thanks!)
public static bool IsAttributeDefined<t>(this ICustomAttributeProvider provider, bool inherit = true) where T : Attribute
{ return provider.IsDefined(typeof(T), inherit); }
public static T[] GetAttributes</t><t>(this ICustomAttributeProvider provider, bool inherit = true) where T : Attribute
{ return provider.GetCustomAttributes(typeof(T), inherit) as T[]; }
public static T GetFirstAttribute</t><t>(this ICustomAttributeProvider provider, inherit = true) where T : Attribute
{ return provider.GetAttributes</t><t>(inherit).FirstOrDefault(); }
}
var instance = new SomeClass();
// How to check for attributes using the extension methods.
bool hasMarkerAttribute = instance.HasAttribute<MyMarkerAttribute>();
// How to check for marker interface using unfair compiler sugar. ;)
bool hasMarkerInterface = instance is IMyMarkerInterface;
// Not that much shorter, right?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment