Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can define a useful extension method ...</p> <pre><code>Type type = typeof(ProjectController); var attributes = type.GetCustomAttributes&lt;CustomDescriptionAttribute&gt;( true ); </code></pre> <p>Here is the extension method:</p> <pre><code>/// &lt;summary&gt;Searches and returns attributes. The inheritance chain is not used to find the attributes.&lt;/summary&gt; /// &lt;typeparam name="T"&gt;The type of attribute to search for.&lt;/typeparam&gt; /// &lt;param name="type"&gt;The type which is searched for the attributes.&lt;/param&gt; /// &lt;returns&gt;Returns all attributes.&lt;/returns&gt; public static T[] GetCustomAttributes&lt;T&gt;( this Type type ) where T : Attribute { return GetCustomAttributes( type, typeof( T ), false ).Select( arg =&gt; (T)arg ).ToArray(); } /// &lt;summary&gt;Searches and returns attributes.&lt;/summary&gt; /// &lt;typeparam name="T"&gt;The type of attribute to search for.&lt;/typeparam&gt; /// &lt;param name="type"&gt;The type which is searched for the attributes.&lt;/param&gt; /// &lt;param name="inherit"&gt;Specifies whether to search this member's inheritance chain to find the attributes. Interfaces will be searched, too.&lt;/param&gt; /// &lt;returns&gt;Returns all attributes.&lt;/returns&gt; public static T[] GetCustomAttributes&lt;T&gt;( this Type type, bool inherit ) where T : Attribute { return GetCustomAttributes( type, typeof( T ), inherit ).Select( arg =&gt; (T)arg ).ToArray(); } /// &lt;summary&gt;Private helper for searching attributes.&lt;/summary&gt; /// &lt;param name="type"&gt;The type which is searched for the attribute.&lt;/param&gt; /// &lt;param name="attributeType"&gt;The type of attribute to search for.&lt;/param&gt; /// &lt;param name="inherit"&gt;Specifies whether to search this member's inheritance chain to find the attribute. Interfaces will be searched, too.&lt;/param&gt; /// &lt;returns&gt;An array that contains all the custom attributes, or an array with zero elements if no attributes are defined.&lt;/returns&gt; private static object[] GetCustomAttributes( Type type, Type attributeType, bool inherit ) { if( !inherit ) { return type.GetCustomAttributes( attributeType, false ); } var attributeCollection = new Collection&lt;object&gt;(); var baseType = type; do { baseType.GetCustomAttributes( attributeType, true ).Apply( attributeCollection.Add ); baseType = baseType.BaseType; } while( baseType != null ); foreach( var interfaceType in type.GetInterfaces() ) { GetCustomAttributes( interfaceType, attributeType, true ).Apply( attributeCollection.Add ); } var attributeArray = new object[attributeCollection.Count]; attributeCollection.CopyTo( attributeArray, 0 ); return attributeArray; } /// &lt;summary&gt;Applies a function to every element of the list.&lt;/summary&gt; private static void Apply&lt;T&gt;( this IEnumerable&lt;T&gt; enumerable, Action&lt;T&gt; function ) { foreach( var item in enumerable ) { function.Invoke( item ); } } </code></pre> <p><strong>Update:</strong></p> <p>Here is a shorter version as proposed by SimonD in a comment:</p> <pre><code>private static IEnumerable&lt;T&gt; GetCustomAttributesIncludingBaseInterfaces&lt;T&gt;(this Type type) { var attributeType = typeof(T); return type.GetCustomAttributes(attributeType, true). Union(type.GetInterfaces(). SelectMany(interfaceType =&gt; interfaceType.GetCustomAttributes(attributeType, true))). Distinct().Cast&lt;T&gt;(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload