当我反序列化以下对象时,出现“在反序列化期间引用跟踪的对象更改了引用”错误:
[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:我发现在 ZmqMessage 对象的 MessageBody 中发送诸如“Test”之类的字符串时没有收到错误消息。但是,当我发送诸如 (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);