Considering these class definitions:
[ProtoContract, ProtoInclude(2, typeof(Class2))]
class Class1
{
[ProtoMember(1)]
public string Field1 { get; set; }
}
[ProtoContract]
class Class2 : Class1
{
[ProtoMember(1)]
public string Field2 { get; set; }
}
I am trying to achieve the following:
using (var ms = new MemoryStream())
{
var c1 = new Class1 { Field1 = "hello" };
Serializer.Serialize<Class1>(ms, c1);
ms.Position = 0;
var c2 = Serializer.Deserialize<Class2>(ms);
}
But I get the following exception: Unable to cast object of type 'ProtoBufTest.Class1' to type 'ProtoBufTest.Class2'
I don't really understand the issue; my understanding is that when deserializing, Protobuf should just consider the incoming stream as a collection of bytes, so why does it apparently deserialize to a Class1
object first, and then try to fit it in a Class2
?