To get this to work, you simply need to assign a tag (number) that it will use to identify the sub-type. Basically, the core "protocol buffers" wire spec doesn't handle inheritance, so protobuf-net achieves it by modelling inheritance as encapsulation. You assign tags on properties/fields with [ProtoMember]
, and sub-types via [ProtoInclude]
(compare to [XmlInclude]
).
Note that the tags must be unique within any single type, but that they can be re-used in sub-types - as shown in the example by both levels using tag 1.
Like so:
using System.Collections.Generic;
using ProtoBuf;
[ProtoContract]
[ProtoInclude(20, typeof(SomeDerived))]
public class SomeBase
{
[ProtoMember(1)]
public string BaseProp { get; set; }
}
[ProtoContract]
public class SomeDerived : SomeBase
{
[ProtoMember(1)]
public int DerivedProp { get; set; }
}
[ProtoContract]
public class SomeEntity
{
[ProtoMember(1)]
public List<SomeBase> SomeList;
}
class Program
{
static void Main()
{
SomeEntity orig = new SomeEntity
{
SomeList = new List<SomeBase> {
new SomeBase { BaseProp = "abc"},
new SomeDerived { BaseProp = "def", DerivedProp = 123}
}
};
var clone = Serializer.DeepClone(orig);
// clone now has a list with 2 items, one each SomeBase and SomeDerived
}
}