是否可以将同一程序集的不同版本引用到单个项目中?

Is it possible to reference different version of the same assembly into a single project?

在我的解决方案中,我有几个使用 Log4Net(1.2 和 2.5)的项目。

然后我有一个项目,我在其中进行(其他项目的)所有单元测试。所以我处于一种情况,取决于我 test/mock 我需要 Log4Net 1.2 或 2.5。

我了解到您可以在应用程序中支持单个程序集的不同版本(使用代码库等),但是否有可能在项目中支持单个程序集的不同版本?如果可以,怎么做?

编辑:

这是一个显示我的问题的小型(2 类、2 个方法、2 个构造函数)项目:

https://srv-file1.gofile.io/download/EQFdOs/212.76.254.142/Log4NetMulti.zip

(我希望 link 工作)

您可以使用 Alias 引用同一 dll 的不同版本,如此处所述 MSDN:

Add the reference to both the dlls in your client application solution. Then in the Solution Explorer under the reference node select the first (old version) class library. In the property window change Aliases field from global to oldVer. Make similar change after selecting the newer class library as well. You are then good to go…

多亏了 Pikoh 的建议,我才得以成功。但是因为它需要的不仅仅是使用别名和重命名dll,所以我会写下整个过程。

  1. 从项目
  2. 中删除现有引用(例如:log4net.dll)
  3. 在项目中添加一个文件夹来存放几个dll(ex: /Libs)
  4. 在里面添加几个dll,并给它们唯一的文件名(比如log4net.1.2.10.0.dll,log4net.1.2.15.0.dll)
  5. 转到这些文件的 属性 并选择“复制:始终”或“复制:如果较新,则复制”
  6. 添加对这些文件的引用
  7. 为这些引用提供唯一的别名(例如:log4net_1_2_10_0、log4net_1_2_15_0)
  8. 在您的代码中,使用别名,使用“extern alias”关键字。

    extern alias log4net_1_2_10_0;
    
    using log4net_1_2_10_0.log4net;
    using System.Web;
    ...
    
  9. 将代码库 添加到您的配置文件中。这些应该引用您放入文件夹中的 dll,具有正确的版本和令牌。

    <runtime> 
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
          <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" />
          <codeBase version="1.2.15.0" href="Libs/log4net.1.2.15.0.dll"/>
        </dependentAssembly>
        <dependentAssembly>
          <assemblyIdentity name="log4net" publicKeyToken="1b44e1d426115821" />
          <codeBase version="1.2.10.0" href="Libs/log4net.1.2.10.0.dll"/>
        </dependentAssembly>
      </assemblyBinding>
    </runtime>