Skip to content

Instantly share code, notes, and snippets.

@feanz
Created December 11, 2013 13:08
Show Gist options
  • Save feanz/7910133 to your computer and use it in GitHub Desktop.
Save feanz/7910133 to your computer and use it in GitHub Desktop.
AttributeExtensions
public static class AttributeExtensions
{
/// <summary>
/// Will return true if the attributeTarget is decorated with an attribute of type TAttribute.
/// Will return false if not.
/// </summary>
/// <typeparam name="TAttribute"></typeparam>
/// <param name="attributeTarget"></param>
/// <returns></returns>
public static bool IsDecoratedWith<TAttribute>(this ICustomAttributeProvider attributeTarget) where TAttribute : Attribute
{
return attributeTarget.GetCustomAttributes(typeof(TAttribute), false).Length > 0;
}
/// <summary>
/// Will return true the first attribute of type TAttribute on the attributeTarget.
/// </summary>
/// <typeparam name="TAttribute"></typeparam>
/// <param name="attributeTarget"></param>
/// <returns></returns>
public static TAttribute GetAttribute<TAttribute>(this ICustomAttributeProvider attributeTarget) where TAttribute : Attribute
{
return (TAttribute)attributeTarget.GetCustomAttributes(typeof(TAttribute), false)[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment