c# - 在 C# 中使用特定版本的 dll

标签 c# dll version gac

我有一个项目,我想在其中使用某个特定版本的 dll。

GAC 包含该 dll 的几个版本(新的和旧的),我想在运行程序时使用旧的。

问题是最新的 dll 总是从 GAC 获取。

你知道是否有办法:

  • 强制使用运行文件夹中的 dll(我在我的解决方案中引用的那个,在调试中运行良好)。
  • 从 GAC 强制使用旧版本的 dll。

谢谢!

最佳答案

您可以在 runtime 节点的 app.config 或 web.config 中使用 binding redirect:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
  </appSettings>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

确保你有正确的 publicKeyToken 并且知道你想将哪个版本重定向到哪个版本。

(您可以使用 this info 检查 DLLpublicKeyToken。)

MSDN Documentation

您还可以使用 Package Manager Console

为整个解决方案生成这些文件
Get-Project -All | Add-BindingRedirect

这将更新所有 app.config 文件并添加绑定(bind)重定向。

关于c# - 在 C# 中使用特定版本的 dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25131753/

相关文章:

C++ WINAPI 通过 GetProcAddress 调用导出函数

php - 确定 PHP 版本合规性的工具

c# - 如何在 .NET Interop.Excel.Workbooks.OpenText 中指定编码

c# - 使用 C# 解析 XML 字符串

c# - 在以下情况下,我应该使用线程安全集合吗?

dll - SSIS 访问 DLL 中的入口点时出现问题

c# - asp.net超链接无法定位url

c++ - LoadLibrary A 在哪里寻找文件?

ruby - 什么版本的 Ruby 2.0.0 是稳定版?

perl - 为什么版本不可打印?