Access to all types, classes and interfaces from multiple projects through reflection in C#

39 views Asked by At

the question I'm about to ask seems to have a solid solution but honestly I couldn't have find it yet. Consider an asp.net core web api application with multiple projects such as Domain Layer, Application Serivce, Presentation, Infrastructure and so on. While trying to dynamically register services through reflection (in Presentation layer), You'll need to access the types defined in Application Service layer. Reflection seems not to have access to types over projects and is restricted to the project being used. I was wondering if you could mind telling me how it's basically done or what are the best practices to do so rather than addressing dll files. your help is highly appreciated!

enter image description here

1

There are 1 answers

0
J.Memisevic On BEST ANSWER

If you wanna get types with reflection from other assemblies you need to use Assemly.Load("path_to_your_assembly") for example:

  Assembly SampleAssembly = Assembly.Load
            ("SampleAssembly");
        // Display all the types contained in the specified assembly.
        foreach (Type oType in SampleAssembly.GetTypes()) {
            Console.WriteLine(oType.Name);

Or easier way would be to use typeof() like this:

Assembly SampleAssembly = typeof(SomeClassOrInterfaceInAssemblyThatYouWishToLoad).Assembly
 // Display all the types contained in the specified assembly.
            foreach (Type oType in SampleAssembly.GetTypes()) {
                Console.WriteLine(oType.Name);