Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all the types in an Assembly that Inherit from a Specific Type C#

Tags:

c#

reflection

How do you get a collection of all the types that inherit from a specific other type?

like image 490
aceinthehole Avatar asked Aug 12 '09 20:08

aceinthehole


People also ask

How do you find the information type of an assembly?

You can use a Type object's methods, fields, properties, and nested classes to find out everything about that type. Use Assembly. GetType or Assembly. GetTypes to obtain Type objects from assemblies that have not been loaded, passing in the name of the type or types you want.

Which of the given methods is used in C# to get the details about information types in an assembly during runtime?

The Assembly class provides a GetTypes() method to retrieve all types within that particular assembly.

What is inheritance in C#?

Inheritance is a feature of object-oriented programming languages that allows you to define a base class that provides specific functionality (data and behavior) and to define derived classes that either inherit or override that functionality.


4 Answers

Something like:

public IEnumerable<Type> FindDerivedTypes(Assembly assembly, Type baseType)
{
    return assembly.GetTypes().Where(t => baseType.IsAssignableFrom(t));
}

If you need to handle generics, that gets somewhat trickier (e.g. passing in the open List<> type but expecting to get back a type which derived from List<int>). Otherwise it's simple though :)

If you want to exclude the type itself, you can do so easily enough:

public IEnumerable<Type> FindDerivedTypes(Assembly assembly, Type baseType)
{
    return assembly.GetTypes().Where(t => t != baseType && 
                                          baseType.IsAssignableFrom(t));
}

Note that this will also allow you to specify an interface and find all the types which implement it, rather than just working with classes as Type.IsSubclassOf does.

like image 93
Jon Skeet Avatar answered Oct 05 '22 05:10

Jon Skeet


The following method will get a collection of types that inherit a type.

C#

public IEnumerable<Type> FindSubClassesOf<TBaseType>()
{   
    var baseType = typeof(TBaseType);
    var assembly = baseType.Assembly;

    return assembly.GetTypes().Where(t => t.IsSubclassOf(baseType));
}

VB.NET

Public Function FindSubClasses(Of TBaseType)() As IEnumerable(Of Type)

    Dim baseType = GetType(TBaseType)
    Dim assembly = baseType.Assembly

    Return From t In assembly.GetTypes() 
           Where t.IsSubClassOf(baseType) 
           Select t

End Function

If you need to include types that implement an interface see @Jon Skeet's answer.

like image 42
Tim Murphy Avatar answered Oct 05 '22 06:10

Tim Murphy


You have to enumerate all types and check for each if it inherits the one you're looking for.

Some code like the one in this question may be useful for you.

like image 40
Thomas Danecker Avatar answered Oct 05 '22 05:10

Thomas Danecker


Consider using this method (written for a PCL):

public IEnumerable<Type> FindDerivedTypes( Assembly assembly, Type baseType )
{
        TypeInfo baseTypeInfo = baseType.GetTypeInfo();
        bool isClass = baseTypeInfo.IsClass, isInterface = baseTypeInfo.IsInterface;

        return
            from type in assembly.DefinedTypes
            where isClass ? type.IsSubclassOf( baseType ) :
                  isInterface ? type.ImplementedInterfaces.Contains( baseTypeInfo.AsType() ) : false
            select type.AsType();
}
like image 41
HappyNomad Avatar answered Oct 05 '22 04:10

HappyNomad