Protobuf.net: how to handle inheritance without [ProtoInclude]

I use Protobuf.net to serialize some classes. I would like to be able to serialize the SuperHero class (below) without specifying [ProtoInclude] in the base class. This is because derived classes are automatically generated, but the base class is missing, so the base class does not directly know about its derived classes.

    [ProtoContract]
    class Person
    {
        [ProtoMember(1)]
        public int Id { get; set; }
        [ProtoMember(2)]
        public string Name { get; set; }
    }

    [ProtoContract]
    class SuperHero : Person
    {
        [ProtoMember(3)]
        public string Powers { get; set; }
    }

I am using the latest version of protobuf.net.

+5
source share
1 answer

: - - , . , - . , , , .., . ; , - , SuperHero Person:

int tag = 7; // why not
Type subType = typeof(SuperHero);

, protobuf-net :

RuntimeTypeModel.Default.Add(typeof(Person), true).AddSubType(tag, subType);

, , [ProtoInclude(...)]

+6

All Articles