如何在运行时添加参考 [英] How To Add Refrence In Run Time

查看:82
本文介绍了如何在运行时添加参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在运行时将程序集(dll)添加为C#项目中的引用??我需要在运行时添加参考.即通常我们通过在解决方案资源管理器中右键单击引用来添加引用,并且我们可以浏览任何dll并将其添加到引用中.我需要使用代码来完成此操作.

how can i add an assembly(dll) as reference in a C# project at run time.?! i need to add a reference at run time.!? i.e normally we add references by Right-Clicking References in Solution Explorer and we can browse for any dll and add it in references. i need to do this using code.

推荐答案

我想您想要的是加载程序集加载 [ Assembly.LoadFrom [反射 [激活器类 [激活器. CreateInstance方法 [ ^ ] .
希望对您有所帮助:)
I guess what you want is load an Assembly[^] at run time.
Assembly.Load[^]
Assembly.LoadFrom[^]
Using Reflection[^] you can loop through its types and instantiate Objects from it, possibly using the Activator Class[^] and Activator.CreateInstance Method[^].
Hope it helps :)


Naerling的解决方案是正确的:引用的程序集是静态的;您所能做的全部事情是在运行时加载组装并使用Reflection.不幸的是,您无法卸载此类程序集,因为您需要诸如可重装插件之类的东西,还需要将程序集加载到单独的Application Domain中并使用IPC,因为Application Domain彼此隔离,就像单独的进程一样.您只能卸载静态或在运行时链接了所有代码的整个应用程序域.

有关更多详细信息,请参阅我过去的解决方案:
创建使用可重载插件的WPF应用程序... [ ^ ],
AppDomain拒绝加载程序集 [使用CodeDom生成代码 [动态加载用户控件 [
The solution by Naerling is correct: the referenced assembly is a static thing; all you can do it loading assembly during run-time and using Reflection. Unfortunately, you cannot unload such assembly, is you need something like reloadable plugins, you will also need to load assembles in a separate Application Domain and use IPC, as Application Domains are isolated from each other the same was as separate processes. You can unload only the whole Application Domain with all its code linked statically or during run-time.

For more detail, see my past solutions:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^],
code generating using CodeDom[^],
Dynamically Load User Controls[^].

—SA


这篇关于如何在运行时添加参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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