如何创建运行时下载的dll(程序集)的动态实例? [英] How to create dynamic instance of runtime downloaded dll (assembly)?

查看:41
本文介绍了如何创建运行时下载的dll(程序集)的动态实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我已创建

1)1个类库,其中只有1个接口

2)其他类库(例如.xyz.dll),它实现了该接口(通过添加接口的引用)。

3)在控制台应用程序中,现在当我点击按钮时,我想创建xyz.dll的对象,以便我已经加载了.dll文件,找到了类名(来自xml文件,为了更安全,我使用了xml文件,我有商店类名),现在当我尝试使用



string className = node [plugin-class]。InnerText; - 结果xyz.xyzcls

类型type = dbDLL.GetType(className); - 但在这里它给出null {值不能为空。

参数名称:类型}

dynamic c = Activator.CreateInstance(type);

所以,我不能创建对象动态。当我将.exe文件移动到其他文件夹或将其复制到桌面上时。

有没有解决方案,请尽快发布。

Thankx

Hello,
I have create
1) 1 class library , which has only 1 interface
2) other class library (eg. xyz.dll) , which implements that interface (by adding reference of interface).
3) In console application, now when i click on button , i want to create object of xyz.dll so for that i have load the .dll file that find the class name (from xml file ,for more security i have used xml file where i have store class name) , now when i try to use

string className = node["plugin-class"].InnerText; -- result xyz.xyzcls
Type type = dbDLL.GetType(className); -- but here it gives null { Value cannot be null.
Parameter name: type }
dynamic c = Activator.CreateInstance(type);
so , i cant create object dynamic.when i move my .exe file to other folder or copy it on desktop.
Is there any solution , post as soon as possible.
Thankx

推荐答案

我成功使用了类似的代码来实现Plug-In模式。这是我的代码:

I used successfully a similar code for implementing a "Plug-In" pattern. Here is my code:
AssemblyName assemblyName = new AssemblyName();
assemblyName.CodeBase = pluginPath; //The full path of your DLL!
Assembly assembly = Assembly.Load(assemblyName);
//
Type objType = assembly.GetType(_pluginClassName);
IPlugin plugin = (IPlugin)Activator.CreateInstance(objType);





请注意,您必须先将DLL作为程序集加载,那么类名必须以完整的命名空间路径作为前缀,如下一个示例所示:MyTool.Logic.MyClass!



Note that you must load the DLL as assembly first, then the class name must be prefixed with the full "namespace path" like in the next example: "MyTool.Logic.MyClass"!


访问此处以了解...



http://support.microsoft.com/kb/837908 [ ^ ]
visit here to understand...

http://support.microsoft.com/kb/837908[^]


其中我们的.exe文件存在(任何文件夹/桌面),复制界面的dll文件,我得到了它的参考。
Where our .exe file is exists(any folder / desktop) , copy interface's dll file and i got the reference of it.


这篇关于如何创建运行时下载的dll(程序集)的动态实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆