1

我正在尝试反序列化一个对象,当我在第一行代码上单击“进入”时,我会返回到表单,就好像我单击了继续一样。If Else 语句从不执行。

c = CType(x.Deserialize(mobjClient.GetStream), Cereal)
If c.text.Length > 0 Then
    RaiseEvent LineReceived(Me, c.text)
Else
    RaiseEvent CardReceived(Me, c)
End If

这是排除的代码

Dim x As New XmlSerializer(GetType(Cereal))
Dim c As New Cereal

从客户端单步执行序列化代码似乎工作正常。这是我的谷物课,以防你们都需要。mobjClient.getStream 是一个 TcpClient 流。

<Serializable> Public Class Cereal
    Public id As Integer
    Public cardType As Type
    Public attacker As String
    Public defender As String
    Public placedOn As String
    Public attack As Boolean
    Public placed As Boolean
    Public played As Boolean
    Public text As String

    Public Sub New()

    End Sub
End Class
4

2 回答 2

1

这就是我修复它的方法.....终于弄清楚了如何正确序列化和反序列化数据。

    Dim c as New Cereal
    c.text = "Testing...."
    Dim bf As New BinaryFormatter
    Dim ms As New MemoryStream
    Dim b() As Byte

    'serializes the Cereal to the created memory stream
    bf.Serialize(ms, c)
    'converts and moves into the byte array
    b = ms.ToArray()
    ms.Close()

    'send the byte array
    SyncLock mobjClient.GetStream
        mobjClient.GetStream.Write(b, 0, b.Length)
    End SyncLock

这是反序列化...

    Dim intCount As Integer

    Try
        SyncLock mobjClient.GetStream
            'returns the number of bytes to know how many to read
            intCount = mobjClient.GetStream.EndRead(ar)
        End SyncLock
        'if no bytes then we are disconnected
        If intCount < 1 Then
            MarkAsDisconnected()
            Exit Sub
        End If

        Dim bf As New BinaryFormatter
        Dim c As New Cereal
        'when data first comes on the stream it is sent to arData, then this puts into memory stream
        Dim ms As New MemoryStream(arData)

        'cut off the memory stream at the end of the data
        ms.SetLength(intCount)
        'deserialize to the created Cereal, giving us a replica of what was sent
        c = bf.Deserialize(ms)
        ms.Close()

        'starts the listener again
        mobjClient.GetStream.BeginRead(arData, 0, 3145728, AddressOf DoRead, Nothing)

        If c.text IsNot Nothing Then
            DisplayText(c.text)
        ElseIf c.OppUserID IsNot Nothing Then
            OnConnected(c.OppUserID, c.OppGender)
        ElseIf c.command IsNot Nothing Then
            OnCommandReceived(c)
        ElseIf c.hasDeck = True Or c.hasBsDeck = True Or c.hasHand = True Then
            OnDeckReceived(c)
        ElseIf c.discard = True Then
            OnDiscard(c)
        Else
            OnCardReceived(c)
        End If

    Catch e As Exception
        MarkAsDisconnected()
    End Try
于 2013-01-09T19:37:03.417 回答
0

序列化程序标有DebuggerStepTrough。如果您想在那里中断,请手动在自定义代码上放置一个断点。

于 2012-11-28T10:18:14.690 回答