Created
December 11, 2013 13:08
-
-
Save feanz/7910133 to your computer and use it in GitHub Desktop.
AttributeExtensions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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