是否有可能有一类独立于版本的DLL参考?有可能、独立、版本、DLL

2023-09-04 02:07:18 作者:胜者为↗王

我想创建编译成一个DLL中的类。此DLL将功能添加到现有的产品。

I would like to create a class that compiles into a single DLL. This DLL would add functionality to an existing product.

要完成这项工作,包含的潜在产品在自定义类引用的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给别人,但我发现该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 perfectly the version of the product. If there isn't a perfect match, then the following error occurs:

无法加载文件或程序集   Product.Web.UI,版本= 3.6.1920.2,   文化=中立,   公钥= dfeaee0e3978ac79或   它的一个依赖。在位于   集清单定义呢   不匹配的程序集引用。   (从HRESULT异常:0x80131040)

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)

我如何产生一个DLL,它是不挑剔版本的参考?

How do I produce a DLL that isn't picky about the version reference?

推荐答案

这是一个很好的解决方案。它解决了类似的问题对我来说。

This is an excellent solution. It solved a similar problem for me.

http://stackoverflow.com/questions/277817/compile-a-version-agnostic-dll-in-net-using-manifests/284462#284462

在的情况下链接不会消亡,关键是处理AppDomain.CurrentDomain.AssemblyResolve事件如下图所示。事件触发一个程序集绑定失败的任何时间,这样你就可以自己解决这个问题,修复版本冲突。

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.

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