两次加载相同的部件,但版本不同 [英] Loading the same Assembly twice but with different version

查看:77
本文介绍了两次加载相同的部件,但版本不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为asm.dll的程序集。

I have one assembly that is called asm.dll.

此程序集的版本为1.0.0.0(在AssemblyInfo.cs中设置了

This assembly has the version 1.0.0.0 (set within AssemblyInfo.cs)

然后我需要在该程序集中做一些代码修改(仍然是asm.dll),将版本升级到2.0.0.0并重新构建。

Then I need do do some code modifications in that assembly (still asm.dll), advance the version to 2.0.0.0 and build it again.

现在,我有两个名为asm.dll的文件,它们在一些代码修改及其版本号方面有所不同。

Now, I have two files named asm.dll that differ with respect to some code modifications and a their version number.

如何在运行时加载这两个文件?

How do I load these two files during runtime?

附录:

正确现在我尝试以下操作:

Right now I am trying the following:

var asm1 = Assembly.LoadFrom("dir1\asm.dll");
var asm2 = Assembly.LoadFrom("dir2\asm.dll");

var types1 = asm1.GetTypes();
var types2 = asm2.GetTypes();

Type type1 = types1.First<Type>(t => t.Name.Equals("myClassIWantToInstantiate"));
Type type2 = types2.First<Type>(t => t.Name.Equals("myClassIWantToInstantiate"));

MyObject myObject1 = (MyObject1)Activator.CreateInstance(type, new object[] { });
MyObject myObject2 = (MyObject2)Activator.CreateInstance(type, new object[] { });

但是我得到以下行为:


  • 第一次调用 Activator.CreateInstance(...)会正确返回 myObject1

第二次调用 Activator.CreateInstance(...)返回再用 myObject1 代替 myObject2

the second call to Activator.CreateInstance(...) returns again myObject1 instead of myObject2

代码编译,程序运行无异常或可观察到的问题,除了我没有得到 myObject2

The code compiles and the program runs without exception or observable problems, except that I do not get myObject2

我知道这个答案,我认为我使用的代码是相同的,只是更新了一点(如果我错了,请纠正我)。

I am aware of this answer and I think the code I used, is the same, only a bit newer (correct me, if I am wrong).

推荐答案

在您的答案中,您对两个对象都使用Activator.CreateInstance-这使用的是全局注册的对象。我相信从特定程序集加载的类型不足以做到这一点。

In your answer, you are using Activator.CreateInstance for both objects - this is using whatever is registered globally. I believe the types loaded from the specific assemblies will not be enough to do this.

在您链接的答案中,使用 Assembly加载了程序集.LoadFile 而不是 LoadFrom ,并且在程序集实例上调用 CreateInstance 使用静态 Activator.CreateInstance 。您是否尝试过?

In the answer you linked, the assemblies are loaded using Assembly.LoadFile rather than LoadFrom, and CreateInstance is called on the assembly instance, rather than using the static Activator.CreateInstance. Have you tried this?

var asm1 = Assembly.LoadFile("dir1\asm.dll");
var asm2 = Assembly.LoadFile("dir2\asm.dll");

MyObject myObject1 = (MyObject)asm1.CreateInstance("myClassIWantToInstantiate");
MyObject myObject2 = (MyObject)asm2.CreateInstance("myClassIWantToInstantiate");

这篇关于两次加载相同的部件,但版本不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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