Gets all types in the given that are decorated with an of the given type . - CSharp System.Reflection

CSharp examples for System.Reflection:Assembly

Description

Gets all types in the given that are decorated with an of the given type .

Demo Code

// Licensed under the Apache License, Version 2.0 (the "License"); 
using System.Reflection;
using System.Linq;
using System.Collections.Generic;
using System;//  w w  w. jav  a 2s. c o  m

public class Main{
    /// <summary>
      /// Gets all types in the given <paramref name="assembly"/> that are decorated with an
      /// <see href="Attribute"/> of the given type <typeparamref name="T"/>.
      /// </summary>
      /// <returns>A list of all matching types. This value will never be null.</returns>
      public static IList<Type> TypesWith<T>( this Assembly assembly ) where T : Attribute
      {
         return assembly.TypesWith( typeof(T) );
      }
      /// <summary>
      /// Gets all types in the given <paramref name="assembly"/> that are decorated with an
      /// <see href="Attribute"/> of the given <paramref name="attributeType"/>.
      /// </summary>
      /// <returns>A list of all matching types. This value will never be null.</returns>
      public static IList<Type> TypesWith( this Assembly assembly, Type attributeType )
      {
         IEnumerable<Type> query = from t in assembly.GetTypes()
                                   where t.HasAttribute( attributeType )
                                   select t;
         return query.ToArray();
      }
}

Related Tutorials