Risking to violate etiquette (my apologies if that is improper), I like to bump this question because it has been floating around for a while without a single answer. I believe this is actually a more serious bug as part of the Protobuf-net library (am willing to apologize a thousand times if it turns out not to be):
I still get above error when trying to deserialize a value type which is part of a DynamicType object. Here are the methods used for serialization/deserialization and the specific class which is being serialized and deserialized along with some simple test code:
[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);
}
}
}
And here some test cases:
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);
The first testMessage deserializes just fine, while the second (using an int in messageBody) throws above error message.