我有一个应用程序,其中包含许多连接到许多不同 I/O 设备的插件 (MEF)。大多数这些插件都有许多托管和非托管 dll。
一家制造商最近发布了新固件和新驱动程序。 API 保持不变。
我认为我可以将两个 dll 版本包含在单独的资源文件夹中,并在应用程序启动时将所需的集复制到输出文件夹中。
(我想我可以制作插件的第二个副本并找出加载正确副本的方法,但我认为复制 DLL 可能会更容易 - 特别是考虑到插件代码未更改)
这是行不通的。
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 已加载或应该已加载但无法加载。
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 个单独版本的应用程序?
EDIT我还应该说,没有必要同时加载两个 DLL。使用一个导致加载一个或另一个 DLL 的参数来启动应用程序就足够了。