Getting all types in a namespace via reflection

Chethan picture Chethan · Sep 17, 2008 · Viewed 171.2k times · Source

How do you get all the classes in a namespace through reflection in C#?

Answer

aku picture aku · Sep 17, 2008

Following code prints names of classes in specified namespace defined in current assembly.
As other guys pointed out, a namespace can be scattered between different modules, so you need to get a list of assemblies first.

string nspace = "...";

var q = from t in Assembly.GetExecutingAssembly().GetTypes()
        where t.IsClass && t.Namespace == nspace
        select t;
q.ToList().ForEach(t => Console.WriteLine(t.Name));