I’d like to use protobuf-net to serialize a derived class as its base class. In other words, I want the serialization process to discard any indication that the type is derived:
[ProtoContract]
class Base
{
[ProtoMember(1)]
public string PublicInfo { get; set; }
}
class Derived : Base
{
public string SecretInfo { get; set; }
}
class Program
{
static void Main(string[] args)
{
Derived d = new Derived()
{
PublicInfo = "public info",
SecretInfo = "secret info"
};
using (var ms = new MemoryStream())
{
Serializer.NonGeneric.Serialize(ms, d as Base);
ms.Seek(0, SeekOrigin.Begin);
Base deserialized = Serializer.Deserialize<Base>(ms);
Console.WriteLine("Deserialized type: " + deserialized.GetType());
Console.WriteLine("Deserialized value: " + deserialized.PublicInfo);
}
Console.ReadLine();
}
}
I’d like the above program to produce
Deserialized type: Base
Deserialized value: public info
but instead I get an exception about “Type is not expected”.
If I add [ProtoContract] to Derived, the PublicInfo field isn’t set. And if I also add [ProtoInclude(2, typeof(Derived))] to Base then the deserialized type is Derived, not Base as I want.
What am I missing? Apologies if I’ve overlooked an answer somewhere else. I think I’m asking for something like the opposite of this question, though I’d rather not have to explicitly add fields via the RuntimeTypeModel.
If your hierarchy isn’t too complex you could think about composing your derived type with a serialization member instead of inheriting from it.
There are some parts of your object that are serializable and some parts are serialization unaware. It’s not a good idea to mix these in one inheritance hierarchy. Because it doesn’t follow the OO concept of specialization. The base class is serializable, the derived class not, however for inheritance the derived class would have to support everything that the base class already supports.