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

拟墨画扇 提交于 2019-12-19 18:57:12

问题


In my solution, I have several projects which use Log4Net (1.2 and 2.5).

Then I have a project where I do all the unit testing (of the other projects). So I'm in a situation where depending on what I test/mock I would need Log4Net 1.2 or 2.5.

I read you could support different version of a single assembly in an application (using codebase etc.) but is it even possible to support different version of a single assembly into a project? If so, how?

EDIt:

Here's a tiny (2 classes, 2 methods, 2 constructors) project showing my issue:

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

(I hope the link work)


回答1:


Thanks to Pikoh's advices I managed to make it work. But since it requires more than just using alias and rename dll, I'll write down the whole process.

  1. Remove the existing reference (ex: log4net.dll) from the project
  2. Add a folder to the project to store the several dll (ex: /Libs)
  3. Add the several dll in it and give them unique file names (like log4net.1.2.10.0.dll, log4net.1.2.15.0.dll)
  4. Go to property on those files and chose "Copy: Always" or "Copy: Copy if newer"
  5. Add references to those files
  6. Give those references unique aliases (ex: log4net_1_2_10_0, log4net_1_2_15_0)
  7. In you code, make use of the aliases, using "extern alias" keywords.

    extern alias log4net_1_2_10_0;
    
    using log4net_1_2_10_0.log4net;
    using System.Web;
    ...
    
  8. Add codebases into your config file. Those should refer to the dll you placed into your folder, with the proper version and token.

    <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>  
    



回答2:


You can reference diferent versions of the same dll using Alias, as is explained here 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…



来源:https://stackoverflow.com/questions/37315589/is-it-possible-to-reference-different-version-of-the-same-assembly-into-a-single

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!