gpt4 book ai didi

c# - 通过配置加载依赖程序集

转载 作者:太空宇宙 更新时间:2023-11-03 14:50:28 24 4
gpt4 key购买 nike

我有一个 visual studio 项目运行得非常好。但是对于将不同的 dll 放在不同的文件夹中的部署提出了新的客户端要求。

我们有一个可以在不同项目中使用的框架 dll。该框架dll依赖于一些第三方dll。因此,当我从我的项目中使用这个 dll 时,每个依赖的 dll 都会在构建时复制到我的本地,因为 CopyLocal 属性为真。

但是现在有了新的要求,我们不能将 CopyLocal 属性设置为 True。客户不想要任何 dll 的本地副本,而是他想要在某个位置与框架相关的 dll。当我这样做时,依赖的 DLL 没有被定位。

我知道我有两个选择:

  1. 我可以将它们放入 GAC,但我不想这样做,因为我希望它们支持 xcopy。
  2. 使用反射(但我不确定这是正确的方法)

我们可以使用配置做任何事情吗??

最佳答案

您可以使用 <probing> configuration element 配置程序集探测路径:

Specifies application base subdirectories for the common language runtime to search when loading assemblies.

来自 MSDN 的示例:

<configuration>  
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="bin;bin2\subbin;bin3"/>
</assemblyBinding>
</runtime>
</configuration>

但是,如果有问题的程序集驻留在应用程序库之外(“这是应用程序正在执行的根位置”),您将拥有 <codeBase> configuration element :

Specifies where the common language runtime can find an assembly.

来自 MSDN 的示例:

<configuration>  
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="myAssembly"
publicKeyToken="32ab4ba45e0a69a1"
culture="neutral" />
<codeBase version="2.0.0.0"
href="http://www.litwareinc.com/myAssembly.dll"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

有关运行时如何定位程序集的确切详细信息,您可以引用 this MSDN article .

正如 OP 所指出的,不幸的是,codeBase 元素是仅对强命名程序集可用的选项。对于私有(private)程序集,您需要一个解决方法。可以找到一些可行的想法in this discussion比如:

我已经测试了后者并且可以确认它有效:

AppDomain.CurrentDomain.AssemblyResolve += (s, e) =>
Assembly.LoadFile(Path.Combine(Settings.Default.AssemblyPath, Path.ChangeExtension(e.Name.Substring(0, e.Name.IndexOf(',')), ".dll")));

关于c# - 通过配置加载依赖程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52003987/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com