Search code examples
c#.net.net-assemblysystem.reflection

Loading Assembly at runtime so that I can access all of it's types


I want to be able to load assembly at run time and being able to use all of it's types in my code.

I know how to instantiate an object by loading an assembly: that is by

assembly = Assembly.LoadFrom(@"C:\Users\c_desaik\Desktop\PowerTool.exe");

            Type type = assembly.GetType("PowerTool.Automation");
            powerTool = Activator.CreateInstance(type);  

But that will allow me to access one object of the entire assembly. But if I want to use other members of the assembly like enums and other static classes then how can I do it?

If I was referencing to the assembly at the compile time then I should be able to do somehting like Assemblyname.membername. But since I am loading this one at runtime. How can I do that?


Solution

  • If you want all types (including private types) use Assembly.GetTypes()

    assembly = Assembly.LoadFrom(@"C:\Users\c_desaik\Desktop\PowerTool.exe");
    
    foreach(Type type in assembly.GetTypes())
        Console.WriteLine(type.ToString());
    

    If you want only public types use GetExportedTypes:

    foreach(Type type in assembly.GetExportedTypes())
        Console.WriteLine(type.ToString());