How do you get a collection of all the types that inherit from a specific other type?
views:
393answers:
2
+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
2009-08-12 20:04:47
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
2009-08-12 21:18:39
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
2009-08-12 20:06:04