C# Find All Derived Types From Assembly

By admin - Last updated: Sunday, November 15, 2009 - Save & Share - 3 Comments

I was always wondering if there is any method to find all derived types for the given type within an assembly. It would be handy to just pass either a class or an interface type and it will return me all its subs. I’ve come out with a method does that. It finds all the derived types of an given type. The given type could be a class(concrete or abstract) or an interface. The derived types could be class, struct or interface.

Here is the data structure that was used for testing,

derived types
From the data structure in the diagram, the tests have been covered with

Console.WriteLine(“All implements MyInterfaceI”);
var result = FindDerivedTypesFromAssembly(Assembly.GetEntryAssembly(), typeof(MyInterfaceI), false);
foreach (var type in result) Console.WriteLine(type.Name);

Console.WriteLine(“All implements MyInterface”);
result = FindDerivedTypesFromAssembly(Assembly.GetEntryAssembly(), typeof(MyInterface), false);
foreach (var type in result) Console.WriteLine(type.Name);

Console.WriteLine(“Only the classes implements MyInterface”);
result = FindDerivedTypesFromAssembly(Assembly.GetEntryAssembly(), typeof(MyInterface), true);
foreach (var type in result) Console.WriteLine(type.Name);

Console.WriteLine(“Only the classes inherits MyClass2″);
result = FindDerivedTypesFromAssembly(Assembly.GetEntryAssembly(), typeof(MyClass2), false);
foreach (var type in result) Console.WriteLine(type.Name);

Console.WriteLine(“Only the classes inherits MyClass3″);
result = FindDerivedTypesFromAssembly(Assembly.GetEntryAssembly(), typeof(MyClass3), false);
foreach (var type in result) Console.WriteLine(type.Name);

Output:

All implements MyInterfaceI
MyInterface
MyClass3
MyClass1
MyClass2
MyClass4
MyClass5
MyStruct1
All implements MyInterface
MyClass3
MyClass1
MyClass2
MyClass4
MyClass5
MyStruct1
Only the classes implements MyInterface
MyClass3
MyClass1
MyClass2
MyClass4
MyClass5
Only the classes inherits MyClass2
MyClass5
Only the classes inherits MyClass3
MyClass4

Here is the extension method does that,

public static IEnumerable<Type> FindDerivedTypesFromAssembly(this Assembly assembly, Type baseType, bool classOnly)
{
    if (assembly == null)
        throw new ArgumentNullException(“assembly”, “Assembly must be defined”);

    if (baseType == null)
        throw new ArgumentNullException(“baseType”, “Parent Type must be defined”);

    // get all the types
    var types = assembly.GetTypes();

    // works out the derived types
    foreach (var type in types)
    {
        // if classOnly, it must be a class
        // useful when you want to create instance
        if (classOnly && !type.IsClass)
            continue;

        if (baseType.IsInterface)
        {
            var it = type.GetInterface(baseType.FullName);

            if (it != null)
                // add it to result list
                yield return type;
        }
        else if (type.IsSubclassOf(baseType))
        {
            // add it to result list
            yield return type;
        }
    }
}

Bugs or issues are much appreciated.




Posted in Code Notes • Tags: Top Of Page

3 Responses to “C# Find All Derived Types From Assembly”

Comment from Alexander
Time April 30, 2010 at 2:40 am

Well done.. Great utility :)

Comment from John
Time October 9, 2010 at 9:30 am

Thank you very much… saved a lot of time :)

Comment from PokeADonkey
Time July 12, 2012 at 9:23 pm

Great article, very much appreciated.

Slight error in the examples though as they should read “Assembly.GetEntryAssembly().FindDerivedTypesFromAssembly(typeof(MyClass3), false)” given that FindDerivedTypesFromAssembly is an extension method.

Write a comment


Captcha: 8 − one =