如何在运行时加载Assembly并创建类实例? [英] How to load Assembly at runtime and create class instance?
本文介绍了如何在运行时加载Assembly并创建类实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个大会.在这个程序集中,我有一个类和接口.我需要在运行时加载此程序集,并希望创建该类的对象,并且还希望使用该接口.
I have a assembly. In this assembly I have a class and interface. I need to load this assembly at runtime and want to create an object of the class and also want to use the interface.
Assembly MyDALL = Assembly.Load("DALL"); // DALL is name of my dll
Type MyLoadClass = MyDALL.GetType("DALL.LoadClass"); // LoadClass is my class
object obj = Activator.CreateInstance(MyLoadClass);
这是我的代码.怎么改善呢?
This is my code. How could it be improved?
推荐答案
如果您的程序集位于GAC或bin中,请在类型名称的末尾使用程序集名称,而不要使用Assembly.Load()
.
If your assembly is in GAC or bin use the assembly name at the end of type name instead of Assembly.Load()
.
object obj = Activator.CreateInstance(Type.GetType("DALL.LoadClass, DALL", true));
这篇关于如何在运行时加载Assembly并创建类实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文