简体   繁体   中英

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?

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM