如何将不同组件中的对象相互转换?

时间:2021-12-17 12:03:19

I've run into this apparently not uncommon problem --

我遇到了这个显然不常见的问题 -

  1. I have a interface in Assembly A.

    我在Assembly A中有一个接口。

  2. I am dynamically loading Assembly B, and trying to cast an object from it into my interface from Assembly A.

    我正在动态加载程序集B,并尝试将其中的对象从程序集A转换到我的接口。

  3. It's failing with an InvalidCastException.

    它失败了InvalidCastException。

I've come to understand why -- a class in Assembly A and a class in Assembly B, even with the same name and the same code, are not the same thing and cannot be cast to one another.

我已经明白了为什么 - 程序集A中的类和程序集B中的类,即使具有相同的名称和相同的代码,也不是相同的东西,不能相互强制转换。

My question is: is there a solution to this? Is there any way I can make the two types compatible, or do I need to duplicate my interface in Assembly B and cast the object to that?

我的问题是:有解决方案吗?有什么方法可以使这两种类型兼容,或者我是否需要在程序集B中复制我的接口并将对象转换为该接口?

I'm loading Assembly B with Assembly.LoadFrom -- is there a different way to do that which would make them type-compatible?

我正在使用Assembly.LoadFrom加载程序集B - 是否有不同的方法可以使它们与类型兼容?

I can't be the first person to have this problem. What I'm trying to do would seem like a fairly common task with plugin architectures.

我不能成为第一个遇到这个问题的人。我想要做的事情似乎是插件架构相当普遍的任务。

2 个解决方案

#1


Duplicating is not the answer. Can Assembly B possibly reference Assembly A and consume the interface (or whatever) from there?

复制不是答案。装配B可能会引用装配A并从那里消耗接口(或其他)吗?

If not, declare the interface (or whatever) in a standalone dll that both Assembly A and Assembly B reference. Then there is only 1 version of the interface, and everything works.

如果没有,请声明程序集A和程序集B引用的独立dll中的接口(或其他)。然后只有一个版本的界面,一切正常。

#2


You can definitely cast from a type in one assembly to a type in another - otherwise we could never cast down from object :)

你绝对可以从一个程序集中的类型转换为另一个程序集中的类型 - 否则我们永远无法从对象中删除:)

I suspect the problem is that the interface has been loaded separately - I suspect that your Assembly B has loaded Assembly A again (possibly from somewhere else?).

我怀疑问题是接口已单独加载 - 我怀疑你的程序集B再次加载了程序集A(可能来自其他地方?)。

What does your file layout look like? If you're loading Assembly B from a different directory and it has a copy of Assembly A in its directory, it may be loading it from there. Assembly loading and binding is a tricky business. I recommend getting hold of "CLR via C#" and reading the chapter on it very carefully, and/or turning on Fusion logging to see what's going on.

你的文件布局是什么样的?如果您从另一个目录加载程序集B并且在其目录中有一个程序集A的副本,则可能从那里加载它。装配和装订是一项棘手的工作。我建议抓住“通过C#CLR”并仔细阅读其中的章节,和/或打开Fusion日志以查看正在发生的事情。

(I'm assuming that the type in assembly B really does implement the interface in assembly A, by the way? If not, that would explain it!)

(我假设程序集B中的类型确实实现了程序集A中的接口,顺便说一下?如果不是,那就解释了它!)

I have a fairly old article which acts as a sort of tutorial for this, by the way. It may help.

顺便说一下,我有一篇相当古老的文章作为一种教程。它可能有所帮助。

#1


Duplicating is not the answer. Can Assembly B possibly reference Assembly A and consume the interface (or whatever) from there?

复制不是答案。装配B可能会引用装配A并从那里消耗接口(或其他)吗?

If not, declare the interface (or whatever) in a standalone dll that both Assembly A and Assembly B reference. Then there is only 1 version of the interface, and everything works.

如果没有,请声明程序集A和程序集B引用的独立dll中的接口(或其他)。然后只有一个版本的界面,一切正常。

#2


You can definitely cast from a type in one assembly to a type in another - otherwise we could never cast down from object :)

你绝对可以从一个程序集中的类型转换为另一个程序集中的类型 - 否则我们永远无法从对象中删除:)

I suspect the problem is that the interface has been loaded separately - I suspect that your Assembly B has loaded Assembly A again (possibly from somewhere else?).

我怀疑问题是接口已单独加载 - 我怀疑你的程序集B再次加载了程序集A(可能来自其他地方?)。

What does your file layout look like? If you're loading Assembly B from a different directory and it has a copy of Assembly A in its directory, it may be loading it from there. Assembly loading and binding is a tricky business. I recommend getting hold of "CLR via C#" and reading the chapter on it very carefully, and/or turning on Fusion logging to see what's going on.

你的文件布局是什么样的?如果您从另一个目录加载程序集B并且在其目录中有一个程序集A的副本,则可能从那里加载它。装配和装订是一项棘手的工作。我建议抓住“通过C#CLR”并仔细阅读其中的章节,和/或打开Fusion日志以查看正在发生的事情。

(I'm assuming that the type in assembly B really does implement the interface in assembly A, by the way? If not, that would explain it!)

(我假设程序集B中的类型确实实现了程序集A中的接口,顺便说一下?如果不是,那就解释了它!)

I have a fairly old article which acts as a sort of tutorial for this, by the way. It may help.

顺便说一下,我有一篇相当古老的文章作为一种教程。它可能有所帮助。

相关文章