AppDomain.CurrentDomain.AppendPrivatePath(“myPath”);替代品?
我正在动态加载 .dll,并且我想从以下子目录加载它们 我的 .exe 所在的位置。
要获得类似 Assembly.Load("SomeAssembly");
的内容,其中 SomeAssembly.dll 位于“DLLs\”下,我已经完成了
AppDomain.CurrentDomain.AppendPrivatePath("DLLs");
这工作正常,但显然 AppendPrivatePath
已弃用。 我被告知取代它的是把它放在我的 app.config 中
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="DLLs"/>
</assemblyBinding>
</runtime>
但是,这没有效果。 Assembly.Load("SomeAssembly")
抛出无法找到 SomeAssembly
的异常。那么我该如何让它发挥作用呢?
我当然可以:
- 继续使用 AppDomain.CurrentDomain.AppendPrivatePath("DLLs"); ,即使它已被弃用。
- 将我的所有插件 .dll 放在与 .exe 相同的目录中(嗯...)
I'm dynamically loading .dlls, and I'd want to load them from a subdirectory of
where my .exe is located.
To get something like Assembly.Load("SomeAssembly");
where SomeAssembly.dll is located under "DLLs\" ,I've done
AppDomain.CurrentDomain.AppendPrivatePath("DLLs");
This works fine, but apparently AppendPrivatePath
is deprecated.
I'm told what replaces it is to place this in my app.config
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="DLLs"/>
</assemblyBinding>
</runtime>
However, this has no effect. Assembly.Load("SomeAssembly")
throws an exception that SomeAssembly
could not be found. So how do I get this working ?
I could ofcourse:
- Continue to use
AppDomain.CurrentDomain.AppendPrivatePath("DLLs");
, even though it's deprecated. - Place all my plugin .dlls in the same directory as the .exe (meh...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用手动程序集解析来执行此操作。
您需要为当前 AppDomain 中的 AssemblyResolve 事件提供委托,
当应用程序具有无法解析的任何程序集引用时,它将调用此委托来解析程序集。然后,您可以简单地返回委托请求的程序集:
希望这有帮助
you can use manual assembly resolution to do this.
You need to provide a delegate to the AssemblyResolve event in the current AppDomain
when the application has any assembly references that it can't resolve it will call this delegate to get the assembly resolved. You can then simply return the assembly requested from the delegate:
hope this helps
不,众所周知
元素工作良好。当您在调试器中运行代码时,您可能会发现这没有效果。这是因为“Visual Studio 托管进程”,这是一个改进调试的 CLR 自定义版本。将 yourapp.exe.config 复制到 yourapp.vshost.exe.config。或者禁用托管进程:“项目 + 属性”、“调试”选项卡。
No, the
<probing>
element is known to work well. You may find this doesn't have an effect when you run the code in the debugger. That's because of the "Visual Studio Hosting process", a customized version of the CLR that improves debugging.Copy yourapp.exe.config to yourapp.vshost.exe.config. Or disable the hosting process: Project + Properties, Debugging tab.