是否可以在类中具有与版本无关的DLL引用? [英] Is it possible to have version-independent DLL references in a class?

查看:179
本文介绍了是否可以在类中具有与版本无关的DLL引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可以编译成单个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,Version = 3.6.1920.2,
Culture = neutral,
PublicKeyToken = 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.

在.NET中编译版本不可知DLL

如果链接死机,关键是来处理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;
        }
    }
}

这篇关于是否可以在类中具有与版本无关的DLL引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆