InvalidCastException的为同一类型的两个对象 [英] InvalidCastException for two Objects of the same type

查看:344
本文介绍了InvalidCastException的为同一类型的两个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我不能处理自己这个奇怪的问题。在我的MVP项目设计为单例的模型类导致一个InvalidCastException。错误的根源在哪里deserialised对象被分配到的类的实例变量的代码行中找到: engineObject =(发动机)xSerializer.Deserialize(STR); 这发生每当我尝试添加我的用户控件中的一个表单或不同的UC。我所有的UCS的有访问单件类的上述实例变量一个特殊的主持人。这是我在尝试添加UC某处时:

I have this weird problem that I cannot handle myself. A class in the model of my mvp-project designed as singleton causes an InvalidCastException. The source of error is found in this code line where the deserialised object is assigned to the instance variable of the class: engineObject = (ENGINE)xSerializer.Deserialize(str); It occurs whenever I try to add one of my UserControls to a Form or to a different UC. All of my UCs have a special presenter that access the above mentioned instance variable of the singleton class. This is what I get when trying to add a UC somewhere:

System.TypeInitializationException:对的类型初始值MVP.Model.EngineData引发了异常。 ----> System.InvalidCastException:[A]发动机不能转换为[B]发动机。类型从起源MVP.Model,版本= 1.0.0.0,文化=中立,公钥=空'在位置上下文LoadNeither''[...] \AppData\Roaming\Microsoft\VisualStudio\\ \\9.0\ProjectAssemblies\uankw1hh01\MVP.Model.dll。 B型从上下文LoadNeither'MVP.Model,版本= 1.0.0.0,文化=中立,公钥=空'的位置[...] \AppData\Roaming\Microsoft\VisualStudio\\起源\\9.0\ProjectAssemblies\u_hge2de01\MVP.Model.dll'...

'System.TypeInitializationException: The type initializer for 'MVP.Model.EngineData' threw an exception. ----> System.InvalidCastException: [A]Engine cannot be cast to [B]Engine. Type A originates from 'MVP.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location '[...]\AppData\Roaming\Microsoft\VisualStudio\9.0\ProjectAssemblies\uankw1hh01\MVP.Model.dll'. Type B originates from 'MVP.Model, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' in the context 'LoadNeither' at location '[...]\AppData\Roaming\Microsoft\VisualStudio\9.0\ProjectAssemblies\u_hge2de01\MVP.Model.dll'...

所以,我不知有两个组件和它们不会从我的项目文件夹访问,但是从VS临时文件夹?我用Google搜索了很多,才发现这一点: IronPython的例外:[A]人不可能转换为[B]人。有提供了一个解决方案,但它首先涉及IronPhyton和第二,我不知道在哪里我的项目中使用它吗?

So I somehow have two assemblies and they are not accessed from my project folder, but from a VS temp folder? I googled a lot and only found this: IronPython Exception: [A]Person cannot be cast to [B]Person. There is a solution offered, but first it concerns IronPhyton and second I don't know where to use it within my project?

这将是好了,如果ü可以帮助我在这里:-)
THX

It would be just great, if u could help me out here :-) thx

推荐答案

类型为每组装;如果有相同的组件装入两次,然后在每一个组件的复制的类型不被认为是同一类型。

Types are per-assembly; if you have "the same" assembly loaded twice, then types in each "copy" of the assembly are not considered to be the same type.

,这些问题通常突然出现当两个组件是在Load和LoadFrom上下文。见

These issues normally crop up when the two assemblies are in the Load and LoadFrom contexts. See

http://stackoverflow.com/questions/1477843/difference-between-loadfile-and-loadfrom-with-net-assemblies

和链接suzcook的博客对这个问题的详细信息。

and the link to suzcook's blog for details on that issue.

此外,考虑使用融合日志查看器,以帮助诊断问题。

Also, consider using the fusion log viewer to help diagnose the problem.

http://msdn.microsoft.com/en-us/library/ e74a18c4%28VS.71%29.aspx

这篇关于InvalidCastException的为同一类型的两个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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