Mike A Mike A - 3 months ago 10
C# Question

How can I search the assemblies for all the classes available in a specific namespace with from a controller using c#?

I need to get all available classes in a giving namespace.

Here is what I have done

In my

Index
method in
XyzController.cs
I added this line.

var classesList = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.Namespace == "PATH.TO.HAMESPACE").ToList()


Unfortunately, that gave me no records/classes.

However, when I created a new class in the the same namespace i.e.
PATH.TO.HAMESPACE
. with the same code. Then called this class from the controller, the code returns the correct classes list.

How can I run this code from the controller to get all available classes with in
PATH.TO.HAMESPACE
?

Answer

Overkill method, but that could work:

AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(a => a.GetTypes())
    .Where(t => t.Namespace == "Namespace");

Or (and), you should try to load explicitly your assembly. With Assembly.Load or Assembly.LoadFrom.

Assembly.Load("your assembly fullname")
    .GetTypes()
    .Where(t => t.Namespace == "Namespace");