使用同一个 DLL 的多个版本 [英] Using multiple versions of the same DLL

查看:35
本文介绍了使用同一个 DLL 的多个版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的任务是为应用程序创建一个新模块,因此,我要向项目添加新的 DLL.这一切都很好.

I've been tasked in creating a new module for an application, and so, I'm adding new DLLs to the project. This is all fine and well.

但是,在我的 DLL 中,我想使用外部 DLL 的新版本(我无法控制).如果我只引用新的 DLL 并仅使用该 DLL,我的代码可以运行,但旧代码将停止运行.

However, in my DLLs I'd like to use a new version of an external DLL (over which I have no control). If I just reference the new DLL and work with that one only, my code will work, but the old code will stop functioning.

Could not load file or assembly 'itextsharp, Version=5.0.6.0, Culture=neutral,
PublicKeyToken=8354ae6d2174ddca' or one of its dependencies. The located assembly's
manifest definition does not match the assembly reference. (Exception from HRESULT:
0x80131040)

我尝试了一个简单的改变 DLL 名称的技巧,但这显然对我来说有点太天真了,认为它会起作用.我已经尝试使用外部别名(通过在我的参考文献中定义它们),但我仍然不知道如何将两个同名文件放入一个 BIN 文件夹中...

I've tried a simple trick of changing the DLLs name, but that apparently was a bit too naive of me, to think it would work. I've tried using the external aliases (by defining them in my references), but I still don't know how to get two files with the same name into one BIN folder...

我该怎么办?

推荐答案

您可以 将另一个版本加载到特定的 AppDomain

可能太详细了,但这里有一篇文章演示了在有用的环境中使用 AppDomains 及其工作原理:

Possibly too detailed, but here is an article that demonstrates the use of AppDomains in a useful setting and how they work:

http://msdn.microsoft.com/en-us/magazine/cc164072.aspx

从一个非常基本的意义上讲,它归结为以下示例代码:

In a very basic sense it comes down to this sample code:

    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
    ...

    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
    {
        if (/*some condition*/)
            return Assembly.LoadFrom("DifferentDllFolder\differentVersion.dll");
        else
            return Assembly.LoadFrom("");
    }

这篇关于使用同一个 DLL 的多个版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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