是否有可能在运行时切换的DLL以便使用一个不同的版本? [英] Is it possible to switch DLLs at runtime so as to use a different version?

查看:121
本文介绍了是否有可能在运行时切换的DLL以便使用一个不同的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含若干插件(MEF),其连接到多个不同的I / O设备的一个应用程序。大多数插件有一些托管和非托管的DLL。

I have an application which contains a number of plugins (MEF) which connect to a number of different I/O devices. Most of these plugins have a number of managed and unmanaged dlls.

一家制造商近日发布了新的固件和新的驱动程序。该API保持相同。

One manufacturer has recently released new firmware and new drivers. The API remains the same.

我以为我可以包括DLL版本在单独的资源文件夹,然后复制所需的设置到输出文件夹时,应用程序启动。

I thought that I could include both dll versions in separate resource folders and copy the required set into the output folder when application starts up.

我想我可以只让一个插件的第二个副本,找出加载一个正确的方式,但我认为复制的DLL可能会更容易 - 特别是考虑到该插件code是不变的)

这是行不通的。

static MyClass() // static constructor
{
    // required version?
    if (envvar.Equals("4") )
    {
        path = "ver4.1.1";
    }
    else
    {
        path = "ver2.5.1";
    }

    // location of drivers
    path = Path.Combine("./Prj/QX", path);
    string[] files = Directory.GetFiles(path);

    // Copy the files and overwrite destination files if they already exist.
    foreach (string s in files)
    {
        string fileName = Path.GetFileName(s);
        string destFile = Path.Combine(".", fileName);
        File.Copy(s, destFile, true);
    }

    // force load of assemby
    Assembly assy = LoadWithoutCache("driver.dll");
    string fn = assy.FullName;
}

static Assembly LoadWithoutCache(string path)
{
    using (var fs = new FileStream(path, FileMode.Open))
    {
        var rawAssembly = new byte[fs.Length];
        fs.Read(rawAssembly, 0, rawAssembly.Length);
        return Assembly.Load(rawAssembly);
    }
}

的错误消息表明原始DLL加载或应该被加载,但不能

The error message suggests that the original DLL was loaded or should have been loaded but could not be.

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

有什么办法来实现我的目标还是做我必须建立2个独立的我的应用程序的版本?

Is there any way to achieve my goal or do I have to build 2 separate versions of my application?

修改 我还应该说,这是没有必要有在同一时间下载这两个DLL文件。这将是足以启动用参数导致的一个或另一个的DLL待加载的应用程序

EDIT I should also say that it is not necessary to have both DLLs loaded at the same time. It would be enough to start the application with a parameter which causes one or the other DLL to be loaded.

推荐答案

你可以做到这一点在几个方面。的一种可能是使用的托管扩展性框架(MEF)。 你甚至可以监视那里的dll文件所在的文件夹,并加载一个新的版本,一旦一个新的可用。

You could do it in several ways. One possibility would be to use the Managed Extensibility Framework (MEF). You could even monitor a folder where the dlls reside and load a new version as soon as a new one becomes available.

另一种可能性是,你可以使用的反射动态运行时加载所需的DLL。

The other possibility is that you can use reflection to dynamically load the dll you want during runtime.

这篇关于是否有可能在运行时切换的DLL以便使用一个不同的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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