views:

537

answers:

1

Using Protobuf/Protobuf-net and two classes, one base class and the other derived from the base.
How would you serialize/deserialize a list ?

For instance:

public class SomeBase
{
    ...
}

public class SomeDerived : SomeBase
{
    ...
}

And the following field to be serialized:

public List<SomeBase> SomeList;

Keep in mind that the list contains SomeBase and SomeDerived objects.

+2  A: 

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
    }
}
Marc Gravell
Excellent answer, as always!
Stecy