/// <summary> /// Gets the attribute from the item /// </summary> /// <typeparam name="T">Attribute type</typeparam> /// <param name="Provider">Attribute provider</param> /// <param name="Inherit"> /// When true, it looks up the heirarchy chain for the inherited custom attributes /// </param> /// <returns>Attribute specified if it exists</returns> public static T Attribute <T>(this ICustomAttributeProvider Provider, bool Inherit = true) where T : Attribute { Contract.Requires <ArgumentNullException>(Provider != null, "Provider"); if (Provider.IsDefined(typeof(T), Inherit)) { var Attributes = Provider.Attributes <T>(Inherit); if (Attributes.Length > 0) { return(Attributes[0]); } } return(default(T)); }
/// <summary> /// Gets the first <see href="Attribute"/> of type <typeparamref name="T"/> associated with the <paramref name="provider"/>. /// </summary> /// <returns>The first attribute found on the source element.</returns> public static T Attribute <T>(this ICustomAttributeProvider provider) where T : Attribute { return(provider.Attributes <T>().FirstOrDefault()); }
/// <summary> /// Gets the first <see href="Attribute"/> of type <paramref name="attributeType"/> associated with the <paramref name="provider"/>. /// </summary> /// <returns>The first attribute found on the source element.</returns> public static Attribute Attribute(this ICustomAttributeProvider provider, Type attributeType) { return(provider.Attributes(attributeType).FirstOrDefault()); }
/// <summary> /// Determines whether the <paramref name="provider"/> element has an associated <see href="Attribute"/> /// of any of the types given in <paramref name="attributeTypes"/>. /// </summary> /// <param name="provider"></param> /// <param name="attributeTypes">The list of attribute types to look for. If this list is <c>null</c> or /// empty an <see href="ArgumentException"/> will be thrown.</param> /// <returns>True if the source element has at least one of the specified attribute types, false otherwise.</returns> public static bool HasAnyAttribute(this ICustomAttributeProvider provider, params Type[] attributeTypes) { return(provider.Attributes(attributeTypes).Count() > 0); }
/// <summary> /// Gets the attribute from the item /// </summary> /// <typeparam name="T">Attribute type</typeparam> /// <param name="Provider">Attribute provider</param> /// <param name="Inherit">When true, it looks up the heirarchy chain for the inherited custom attributes</param> /// <returns>Attribute specified if it exists</returns> public static T Attribute <T>(this ICustomAttributeProvider Provider, bool Inherit = true) where T : Attribute { return(Provider.IsDefined(typeof(T), Inherit) ? Provider.Attributes <T>(Inherit)[0] : default(T)); }
public static T Attribute <T>(this ICustomAttributeProvider provider, bool inherit = true) where T : Attribute { return(provider.Attributes <T>(inherit).SingleOrDefault()); }
public static bool HasAttribute <T>(this ICustomAttributeProvider provider, bool inherit = true) where T : Attribute { return(provider.Attributes <T>(inherit).Any()); }