Getting all types in a namespace via reflection


Question

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

1
245
2/4/2012 5:02:51 AM

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));
297
11/9/2016 10:28:05 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon