tags:

views:

393

answers:

2

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

+16  A: 

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 :)

Jon Skeet
Thanks! I ended up using this Thanks - I ended up using this public static List<Type> GetAllSubclassesOf(Type baseType) { return Assembly.GetAssembly(baseType).GetTypes(). Where(type => type.IsSubclassOf(baseType)). ToList(); }
aceinthehole
A: 

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.

Thomas Danecker