C# ILMerge Unable to cast object of type 'ClassY' to type '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 = 开发者_StackOverflow社区(ClassX)foo;
Does anyone have any idea why it would do this? ClassY
definitely implements ClassX
, all assembly/library versions are exactly the same.
All the DLLs must be marked COM visible. Without it, the compiler could not resolved different assembly types -- even they are named the same.
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}'.
精彩评论