C# ILMerge 无法转换类型为“ClassY”的对象输入“ClassX”
情况如下:我正在使用 ILMerge 来合并程序集和程序集。使用 此方法对 1 .dll 文件的所有引用(custom.dll
)。我有一个在运行时动态加载该程序集的应用程序(program.exe
)。应用程序和程序集使用通用库(common.dll
)。抽象类 ClassX
在 common.dll
中定义,而实现 ClassY
在 custom.dll
中定义。当program.exe
尝试从custom.dll
加载ClassY时,它会抛出异常:
无法将“ClassY”类型的对象转换为“ClassX”类型。
代码是这样的,但是 foo 是动态加载的,而不是直接 new ClassY();
object foo = new ClassY();
ClassX bar = (ClassX)foo;
有谁知道为什么会这样做? ClassY
肯定实现了 ClassX
,所有程序集/库版本都完全相同。
Here's the situation: I am using ILMerge to merge an assembly & all it's references into 1 .dll file using this method (custom.dll
). I have an application which dynamically loads this assembly at runtime (program.exe
). Both application & assembly use a common library (common.dll
). Abstract class ClassX
is defined in common.dll
whilst implementation ClassY
is defined in custom.dll
. When program.exe
tries to load ClassY from custom.dll
it throws the exception:
Unable to cast object of type 'ClassY' to type 'ClassX'.
The code is like this, but foo
is dynamically loaded rather than just a straight new ClassY();
object foo = new ClassY();
ClassX bar = (ClassX)foo;
Does anyone have any idea why it would do this? ClassY
definitely implements ClassX
, all assembly/library versions are exactly the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有 DLL 必须标记为 COM 可见。如果没有它,编译器就无法解析不同的程序集类型——即使它们的名称相同。
All the DLLs must be marked COM visible. Without it, the compiler could not resolved different assembly types -- even they are named the same.
你检查过内部异常吗?由于您的 ClassY 是动态加载的,因此当您进行强制转换时,它可能会尝试加载预合并的程序集之一。内部异常是“无法找到程序集'{you're pre-merged assembly name}”。
Have you checked the inner exception? It's possible that since your ClassY is dynamically loaded that when you do the cast it's trying to load one of the pre-merged assemblies. The inner exception would be "Unable to fin assembly '{you're pre-merged assembly name}'.