17
    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.

kristian
  • 22,731
  • 8
  • 50
  • 78
Carlsberg
  • 659
  • 2
  • 7
  • 8
  • 1
    at design or run time? If design then you can refer to this question for some tips http://stackoverflow.com/questions/282377/visual-studio-how-do-i-show-all-classes-inherited-from-a-base-class – Marek Karbarz Nov 03 '09 at 03:48

1 Answers1

29

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)));
nawfal
  • 70,104
  • 56
  • 326
  • 368
Sam Harwell
  • 97,721
  • 20
  • 209
  • 280