转换为相同类型的错误 [英] Casting to same type error

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

问题描述

您好,

当我从文件中反序列化对象时,我得到了奇怪的转换错误。

I am getting weird casting error when I de-serialize an object from a file.

这是堆栈跟踪:

   System.ArgumentException: Object of type 'QAliber.TestModel.TestCase[]' cannot be converted to type 'QAliber.TestModel.TestCase[]'.
   at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
   at System.Reflection.RtFieldInfo.InternalSetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture, Boolean doVisibilityCheck, Boolean doCheckConsistency)
   at System.Reflection.RtFieldInfo.InternalSetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, CultureInfo culture, Boolean doVisibilityCheck)
   at System.Runtime.Serialization.FormatterServices.SerializationSetValue(MemberInfo fi, Object target, Object value)
   at System.Runtime.Serialization.ObjectManager.CompleteObject(ObjectHolder holder, Boolean bObjectFullyComplete)
   at System.Runtime.Serialization.ObjectManager.DoNewlyRegisteredObjectFixups(ObjectHolder holder)
   at System.Runtime.Serialization.ObjectManager.RegisterObject(Object obj, Int64 objectID, SerializationInfo info, Int64 idOfContainingObj, MemberInfo member, Int32[] arrayIndex)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.RegisterObject(Object obj, ParseRecord pr, ParseRecord objectPr, Boolean bIsString)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ParseObjectEnd(ParseRecord pr)
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Parse(ParseRecord pr)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
   at QAliber.TestModel.TestScenario.Load(String filename) in F:\Project\Qaliber Stuff\Qaliber Tool\QAliber Engine\TestModel\TestScenario.cs:line 198
   at QAliber.Builder.Presentation.TabbedScenarioControl.openToolStripButton_Click(Object sender, EventArgs e) in F:\Project\Qaliber Stuff\Qaliber Tool\QAliber Test Builder\Presentation\CustomControls\TabbedScenarioControl.cs:line 120}




我想通过.NET源代码进行调试。但是,我找不到符号文件。

I wanted to debug through .NET source code. However, I could not find symbol files for the same.

我正在使用visual studio 2008和.NET framework 3.5

I am using visual studio 2008 and .NET framework 3.5

可能是什么原因这个问题?

What can be the cause of this issue?

提前谢谢!

Navnath

推荐答案

一个常见的误解是类型A和类型B是相同的,因为它们具有相同的类型名称。这是错的。如果完全限定类型名称(包括类型,名称空间和程序集)相同,则CLR仅认为两种类型相同。
在您的情况下,它们的类型不同。

A common misconception is that type A and type B are the same because they have the same type name. This is wrong. The CLR only considers 2 types to be the same if the fully qualified type names (which includes type, namespace and assembly) are the same. It appears in your case that they aren't the same type.

您可以通过执行以下操作来确认。

You can confirm this by doing the following.

1 )直接在代码中创建您的类型的实例。

1) Create an instance of your type directly in your code.

2)将您遇到问题的值反序列化为对象。

2) Deserialize the value you're having an issue with into an object.

3)比较GetType()在每个对象上返回的类型。

3) Compare the types returned by GetType() on each of the objects.

在我看来,BinaryFormatter不是一个很好的序列化选项。当类型发生变化时,它确实表现不佳。但您可以使用

AssemblyFormat
,看看你的问题是否消失了。它稍微松开了类型检查。

BinaryFormatter isn't a great serialization option in my opinion. It really doesn't behave well when the types are changing. But you might be able to use the AssemblyFormat and see if your problem goes away. It loosens up the type checking a little bit.


这篇关于转换为相同类型的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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