Something like:
public IEnumerable<Type> FindDerivedTypes(Assembly assembly, Type baseType) { return assembly.GetTypes().Where(t => baseType.IsAssignableFrom(t)); }
If you need to handle generics, this becomes a bit more complicated (for example, passed as an open type List<> , but it expects a type returned from List<int> ). Otherwise it's easy though :)
If you want to exclude the type itself, you can do it quite easily:
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 types that implement it, rather than just working with classes like Type.IsSubclassOf .
Jon Skeet Aug 12 '09 at 20:04 2009-08-12 20:04
source share