visual-studio 是否可以在类中拥有与版本无关的 DLL 引用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/861010/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to have version-independent DLL references in a class?
提问by Gabe Sumner
I would like to create a class that compiles into a single DLL. This DLL would add functionality to an existing product.
我想创建一个编译成单个 DLL 的类。此 DLL 将向现有产品添加功能。
To make this work, the custom class references DLLs contained in the underlying product. These references are needed to compile.
为了实现这一点,自定义类引用包含在基础产品中的 DLL。编译需要这些引用。
Everything works fine here and the custom class compiles. I can drop the DLL produced into the product and everything works fine.
这里一切正常,自定义类编译。我可以将生成的 DLL 放入产品中,并且一切正常。
However, this product has several versions (minor versions, service packs). I would like to distribute this DLL to others but I'm finding the DLL must match perfectlythe version of the product. If there isn't a perfect match, then the following error occurs:
但是,该产品有多个版本(次要版本、服务包)。我想将此 DLL 分发给其他人,但我发现 DLL 必须与产品版本完美匹配。如果没有完美匹配,则会出现以下错误:
Could not load file or assembly 'Product.Web.UI, Version=3.6.1920.2, Culture=neutral, PublicKeyToken=dfeaee0e3978ac79' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
无法加载文件或程序集“Product.Web.UI,版本=3.6.1920.2,文化=中性,PublicKeyToken=dfeaee0e3978ac79”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)
How do I produce a DLL that isn't picky about the version reference?
我如何生成一个对版本参考不挑剔的 DLL?
回答by Yoenhofen
This is an excellent solution. It solved a similar problem for me.
这是一个很好的解决方案。它为我解决了类似的问题。
Compile a version agnostic DLL in .NET
In case that link ever dies, the key is to handle the AppDomain.CurrentDomain.AssemblyResolve event like below. The event fires any time an assembly binding fails, so you can resolve it yourself, fixing version conflicts.
如果该链接失效,关键是处理 AppDomain.CurrentDomain.AssemblyResolve 事件,如下所示。每当程序集绑定失败时都会触发该事件,因此您可以自己解决它,修复版本冲突。
using System.Reflection;
static Program()
{
    AppDomain.CurrentDomain.AssemblyResolve += delegate(object sender, ResolveEventArgs e)
    {
        AssemblyName requestedName = new AssemblyName(e.Name);
        if (requestedName.Name == "Office11Wrapper")
        {
            // Put code here to load whatever version of the assembly you actually have
            return Assembly.LoadFile("Office11Wrapper.DLL");
        }
        else
        {
            return null;
        }
    }
}
回答by Gabe Sumner
I don't yet have an answer to my question, but I'll use this answer to log breadcrumbs I found while searching for a solution.
我还没有回答我的问题,但我会使用这个答案来记录我在寻找解决方案时发现的面包屑。
I found a somewhat related question on StackOverflow:
我在 StackOverflow 上发现了一个有点相关的问题:
Compile a version agnostic .DLL in .NET (Using Manifests?)
I have no ability to modify the underlying product however, so the answer does not work for me.
但是,我无法修改基础产品,因此答案对我不起作用。
Updated:
更新:
I emailed someone much smarter than me and here was the reply:
我给比我聪明得多的人发了邮件,回复如下:
When you reference strong-named assembly, by default Visual Studio adds full reference to the referenced assembly. That means it includes the name of the assembly, the exact version, the culture and the public key token. If any of this information don't match the described exception is thrown.
当您引用强命名程序集时,默认情况下 Visual Studio 添加对被引用程序集的完整引用。这意味着它包括程序集的名称、确切版本、文化和公钥标记。如果这些信息中的任何一个与所描述的异常不匹配,则会引发异常。
Removing the strong-names of our assemblies is simply not an option. I won't go in details why, but you can do some research in MSDN.
删除程序集的强名称根本不是一种选择。我不会详细说明原因,但您可以在 MSDN 中进行一些研究。
So, you have two options to workaround building against every version of the assemblies you are referencing.
因此,您有两个选项可以解决针对您所引用的程序集的每个版本进行构建。
- You could do partial referencing. See this article: http://msdn.microsoft.com/en-us/library/0a7zy9z5(VS.71).aspx.
- You can declare compatible versions with binding redirection in the web.config. See this article: http://msdn.microsoft.com/en-us/library/433ysdt1.aspx.
- 你可以做部分引用。请参阅这篇文章:http: //msdn.microsoft.com/en-us/library/0a7zy9z5(VS.71).aspx。
- 您可以在 web.config 中声明具有绑定重定向的兼容版本。请参阅这篇文章:http: //msdn.microsoft.com/en-us/library/433ysdt1.aspx。
In general the second approach is recommended because: 1. You cannot use partial reference to assemblies in the Global Assembly cache, meaning your control will throw the same exception if assemblies are in the GAC. 2. You explicitly state compatible versions.
通常推荐使用第二种方法,因为: 1. 您不能在全局程序集缓存中使用对程序集的部分引用,这意味着如果程序集在 GAC 中,您的控件将抛出相同的异常。2. 您明确说明兼容版本。
回答by Gabe Sumner
In VisualStudio - have you tried right clicking on the referenced assembly (dll) and then selected properties and set "requires specific version" (or so) yet? That may solve your issue.
在 VisualStudio 中 - 您是否尝试过右键单击引用的程序集(dll),然后选择属性并设置“需要特定版本”(左右)?那可能会解决您的问题。
Andreas
安德烈亚斯

