以非特定版本的程序集为目标

Target non specific version of an assembly(以非特定版本的程序集为目标)
本文介绍了以非特定版本的程序集为目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I m trying to not target a specific version of a dll but I m not quite sure how. I have set the option Specific Version on the properties of the assembly to false, however if i try to run the application and the version of the requested assembly is a previous one, I get a:

FileLoadException: Could not load file or assembly 

This is happening when the version of the referenced dll does not exactly match the current one. I would believe that the issue is on how to reference this assembly.

解决方案

In general, if you are trying to use a specific version of an assembly the below doesn't really apply, you should just use the version you need.

However, sometimes you can run into a situation where you have this:

AssemblyX - references version 1.2.1 of AssemblyZ
AssemblyY - references version 1.2.2 of AssemblyZ

But your project needs both AssemblyX and AssemblyY.

So how do you resolve this? You can either put 1.2.1 and 1.2.2 of AssemblyZ in the GAC, or, if you're sure there aren't any compatibility issues, you can use assembly rebinding. Here's an example (this goes in your Web.config or App.config file):

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="myAssembly"
                              publicKeyToken="32ab4ba45e0a69a1"
                              culture="neutral" />
            <bindingRedirect oldVersion="1.0.0.0"
                             newVersion="2.0.0.0"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

What this basically says is that if any assemblies in your solution reference 1.0.0.0 of myAssembly, then they should really use version 2.0.0.0. And you're expected to have version 2.0.0.0 present in the path.

A hack you can use when you always want them to use a specific version of the assembly is to specify a version range, like this:

        <dependentAssembly>
            <assemblyIdentity name="MyAssembly" publicKeyToken="B7567367622062C6" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="1.2.1.0" />
        </dependentAssembly>

This will force version 1.2.1.0 of MyAssembly to be used for any version reference of MyAssembly between 0.0.0.0 and 3.0.0.0.

这篇关于以非特定版本的程序集为目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)