Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all the inherited classes of a base class? [duplicate]

    class Foo { }

    class Foo1 : Foo { }

    class Foo2 : Foo { }

How would I be able to get all the classes that use Foo as a base class? The inherited classes aren't necessary in the same assembly.

like image 619
Carlsberg Avatar asked Nov 03 '09 03:11

Carlsberg


1 Answers

This is not fast, but as long as Foo is a concrete type (not an interface), then it should work. Foo itself is not returned by this code.

AppDomain.CurrentDomain.GetAssemblies()
                       .SelectMany(assembly => assembly.GetTypes())
                       .Where(type => type.IsSubclassOf(typeof(Foo)));
like image 138
Sam Harwell Avatar answered Sep 20 '22 04:09

Sam Harwell