2011-01-12 68 views
1

我正在反序列化对象,并且在某些情况下它可以正常工作,但是在其他情况下它会失败。这个例外对我来说基本上没有意义,但是必须有一种方法来确定它到底发生了什么,所以我重定向了我的调试。如何确定对象图中的哪个对象导致SerializationException

这是例外:

System.Runtime.Serialization.SerializationException 被抓消息= “否地图为 对象 '201326592'。”
Source =“mscorlib”StackTrace: at System.Runtime.Serialization.Formatters.Binary._ BinaryParser.ReadObject() at System.Runtime.Serialization.Formatters.Binary。 _BinaryParser.Run() 在System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler 处理程序,__BinaryParser serParser, 布尔FCHECK,布尔 isCrossAppDomain,IMethodCallMessage methodCallMessage) 在System.Runtime.Serialization.Formatters .Binary.BinaryFormatter.Deserialize(流 serializationStream,HeaderHandler 处理程序,布尔FCHECK,布尔 isCrossAppDomain,IMethodCallMessage methodCallMessage) 在System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(流 serializationStream) 在安娜lytics.ReportInstance.Open(流 T流,布尔OpenResults)在 C:\用户...路径... \ File.vb:线 149的InnerException:

而这是源代码,其中它被困:

Public Shared Function Open(ByVal tStream As IO.Stream, Optional ByVal OpenResults As Boolean = False) As ReportInstance 
     Dim tFormatter As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter = PluginSerializationBinder.CreateSerializer() 
     Dim tInstance As ReportInstance 
     Try 
      If OpenResults Then 'case which always fails 
       'open the entire report 
       If System.Diagnostics.Debugger.IsAttached Then 
        Console.WriteLine("Deserializing: report results") 
       End If 
       tInstance = tFormatter.Deserialize(tStream) 'throws exception here 
       Return tInstance 
      Else 'case which always works (only deserializing part of the object) 
       'just open the stub 
       If System.Diagnostics.Debugger.IsAttached Then 
        Console.WriteLine("Deserializing: report instance") 
       End If 
       Dim tInput As New IO.BinaryReader(tStream) 
       Dim tLength As Long = tInput.ReadInt64() 
       Dim tBytes() As Byte = tInput.ReadBytes(tLength) 
       Dim tMem As New IO.MemoryStream(tBytes) 
       tInstance = tFormatter.Deserialize(tMem) 
       Return tInstance 
      End If 
     Catch ex As Exception 
      If (ex.Message.Contains("blah")) Then 
       Throw New UnsupportedException(ex.Message) 
      Else 
       Throw 'trapped here 
      End If 
     End Try 
    End Function 

感谢, 布赖恩

回答

1

你看到的异常被抛出时,对象的“映射ID” - 一个整数,标识其类型,应该在前面引用类型定义流 - 不是在类型表中找到。

通常情况下,这应该不会发生,除非字节流在传输过程中被损坏 - 或格式化程序实例被不当使用。

A BinaryFormatter跟踪它已经处理的所有内容,并且可以反向引用先前写入的类型和对象(序列化时)或使用先前读取的数据解析当前数据中的反向引用(反序列化时)。

+0

那么你如何解决它? – hofnarwillie 2012-11-28 11:56:58

相关问题