Protobuf-net“参考跟踪对象在deserializartion期间更改了引用”错误

时间:2012-07-17 13:14:32

标签: c# serialization deserialization protocol-buffers protobuf-net

我正在获得一个"参考跟踪对象在deserializartion"我反序列化以下对象时出错:

[ProtoContract]
public class ZmqMessage
{
    [ProtoMember(1)]
    public ZmqMessageType MessageType { get; set; }
    [ProtoMember(2, DynamicType = true)]
    public object MessageBody { get; set; }

    public ZmqMessage()
    { }

    public ZmqMessage(ZmqMessageType zmqMessageType, object messageBody)
    {
        this.MessageType = zmqMessageType;
        this.MessageBody = messageBody;
    }
}

我通过以下方式序列化和反序列化:

public static class ProtoBuf
{
    public static byte[] Serialize<T>(T serializeThis)
    {
        using (var stream = new MemoryStream())
        {
            Serializer.Serialize<T>(stream, serializeThis);
            return stream.GetBuffer();
        }
    }

    public static T Deserialize<T>(byte[] byteArray)
    {
        using (var stream = new MemoryStream(byteArray))
        {
            return Serializer.Deserialize<T>(stream);
        }
    }
}

我在这里做错了什么? 感谢

EDIT1:我发现当我发送&#34;测试&#34;等字符串时,我没有收到错误。在ZmqMessage对象的MessageBody中。但是当我发送一个像(int)1或只是1的int时,它会抛出错误。

EDIT2:这是枚举和快速测试案例,用于演示问题:

public enum ZmqMessageType
{
    RawByteArray = 5550,
    ControlMessage = 5551
}

ZmqMessage testMessage = new ZmqMessage(ZmqMessageType.ControlMessage, "Test");
        byte[] byteMessage = ProtoBuf.Serialize<ZmqMessage>(testMessage);
        ZmqMessage deserializedMessage = ProtoBuf.Deserialize<ZmqMessage>(byteMessage);

ZmqMessage testMessage = new ZmqMessage(ZmqMessageType.ControlMessage, (int) 1);
        byte[] byteMessage = ProtoBuf.Serialize<ZmqMessage>(testMessage);
        ZmqMessage deserializedMessage = ProtoBuf.Deserialize<ZmqMessage>(byteMessage);

0 个答案:

没有答案