12

How can I list all the classes in my current project(assembly?) using reflection? thanks.

ryudice
  • 35,196
  • 32
  • 106
  • 157

3 Answers3

20

Given an instance a of System.Reflection.Assembly, you can get all types in the assembly using:

var allTypes = a.GetTypes();

This will give you all types, public, internal and private.

If you want only the public types, you can use:

var publicTypes = a.GetExportedTypes();

If you are running this code from within the Assembly itself, you can get the assembly using

var a = Assembly.GetExecutingAssembly();

GetTypes and GetExportedTypes will give you all types (structs, classes, enums, interfaces etc.) so if you want only classes you will have to filter

var classes = a.GetExportedTypes().Where(t => t.IsClass);
Mark Seemann
  • 218,019
  • 46
  • 414
  • 706
3

Have a look at the Assembly.GetTypes method.

Gregory Pakosz
  • 67,118
  • 19
  • 136
  • 163
2

Yes, you use the Assembly.GetTypes method.

Nick
  • 5,805
  • 1
  • 25
  • 38