"Protobuf-net inheritance doubt"



I was trying to develop a regular idiotic example in order to learn about this great idea.

but it seems to be missing something as i am not managing to obtain the values from the properties inside the deserialized object...

class Program
{
    static void Main(string[] args)
    {
        Human man =
            new Human()
            {
                Name = "Clark Kent",
                Age = 150
            };

        Stream stream = new MemoryStream();

        Serializer.Serialize(stream, man);

        Human manClone =
            Serializer.Deserialize(stream);

        SuperHuman superman =
            new SuperHuman()
            {
                Name = "Clark Kent",
                Age = 150,
                Power = PowerType.Fly & PowerType.HeatVision & PowerType.SuperSpeed,
                Class = 5
            };
        Stream superStream = new MemoryStream();
        Serializer.Serialize(superStream, superman);

        Human notSoSuperClone =
            Serializer.Deserialize(superStream);
    }
}

[Serializable, ProtoContract]
[ProtoInclude(1, typeof(SuperHuman))]
internal class Human
{
    [ProtoMember(2)]
    internal int Age { get; set; }
    [ProtoMember(3)]
    internal string Name { get; set; }
}

[Serializable, ProtoContract]
internal class SuperHuman : Human
{
    [ProtoMember(4)]
    internal PowerType Power { get; set; }
    [ProtoMember(5)]
    internal byte Class { get; set; }
}

[Flags]
internal enum PowerType
{
    [ProtoEnum]
    Fly = 0,
    [ProtoEnum]
    HeatVision,
    [ProtoEnum]
    SuperSpeed,
    [ProtoEnum]
    SuperHunger
}

Can someone help me please?

Avatar

noproblembabe

almost 4 years ago
 

The biggest problem is simply that you haven't rewound the stream; i.e. set superStream.Position = 0 before deserializing.

(I'm the author of protobuf-net; I don't "hang out" here; but if you have more questions, please find me at stackoverflow)

Avatar

mgravell

almost 4 years ago
 



 

Creative Commons License Copyright © 2013 Black Duck Software, Inc. and its contributors, Some Rights Reserved. Unless otherwise marked, this work is licensed under a Creative Commons Attribution 3.0 Unported License . Ohloh ® and the Ohloh logo are trademarks of Black Duck Software, Inc. in the United States and/or other jurisdictions. All other trademarks are the property of their respective holders.