Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get all classes in current project using reflection? [duplicate]

Tags:

c#

.net

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

like image 251
ryudice Avatar asked Feb 23 '10 14:02

ryudice


3 Answers

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);
like image 172
Mark Seemann Avatar answered Oct 21 '22 19:10

Mark Seemann


Have a look at the Assembly.GetTypes method.

like image 3
Gregory Pakosz Avatar answered Oct 21 '22 21:10

Gregory Pakosz


Yes, you use the Assembly.GetTypes method.

like image 2
Nick Avatar answered Oct 21 '22 20:10

Nick