我可以指定相关目录时动态加载程序集? [英] Can I specify dependency directories when dynamically loading assemblies?

查看:165
本文介绍了我可以指定相关目录时动态加载程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果这样的设置是可能的:

I'm wondering if a setup like this is possible:

C:\ EFLOW \ proxy.dll(由应用程序加载主DLL) C:\ EFLOW \程序\ dynamic.dll(DLL动态地proxy.dll加载) C:\ EFLOW \ dependency.dll(由dynamic.dll需要依赖的DLL)

c:\eflow\proxy.dll (main DLL loaded by application) c:\eflow\application\dynamic.dll (DLL dynamically loaded by proxy.dll) c:\eflow\dependency.dll (dependent DLL required by dynamic.dll)

基本上,我想动态加载DLL(实例化类等),但有一个存储在不同的位置DLL的依赖。

Basically, I'd like to dynamically load a DLL (to instantiate classes, etc) but have that DLL's dependencies stored in a different location.

这可能吗?我不希望有这些相关的DLL中的每一个子目录拷贝......(我不能加载它们在GAC,因为它们没有签名,他们是第三方的DLL)

Is this possible? I don't want to have a copy of these dependent DLLs in every sub-directory... (and I can't load them in the GAC because they aren't signed and they are 3rd party DLLs)

推荐答案

是的,你可以做到这一点。

Yes you can do this.

通过处理 AppDomain.AssemblyResolve事件您可以加载任何你喜欢的依赖程序集。如果你的应用程序知道相关的组件可以位于另一个目录中,然后就可以处理这个事件(自动意味着他们没有在applciation目录中找到作为.NET框架看起来有第一),并尝试从加载程序集其他位置。

By handling the AppDomain.AssemblyResolve Event you can load dependant assemblies from wherever you like. If your application knows that dependant assemblies may be located in another directory then it can handle this event (which automatically implies that they were not found in the applciation directory as the .Net framework looks there first), and attempt to load the assembly from that alternate location.

请参阅解决大会载荷的更多细节。

更新:在这种情况下,我beleive可以代替定目录添加到的 AppDomainSetup.PrivateBinPath物业获取所需的应用程序域。这如果给定的目录是应用程序根目录的子目录中才起作用。如果不是的话,那么使用这种方法,你需要创建一个新的AppDomain用合适的应用程序根目录。

Update: In this case I beleive that you can instead add the given directory to the AppDomainSetup.PrivateBinPath Property for the desired app domain. This will only work if the given directories are subdirectories of the application base directory. If this is not the case then to use this approach you would need to create a new AppDomain with a suitable application base directory.

另请参见最佳实践组件加载

这篇关于我可以指定相关目录时动态加载程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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