Forums : The Ohcount Project

Dear Open Hub Users,

We’re excited to announce that we will be moving the Open Hub Forum to https://community.blackduck.com/s/black-duck-open-hub. Beginning immediately, users can head over, register, get technical help and discuss issue pertinent to the Open Hub. Registered users can also subscribe to Open Hub announcements here.


On May 1, 2020, we will be freezing https://www.openhub.net/forums and users will not be able to create new discussions. If you have any questions and concerns, please email us at info@openhub.net

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?

noproblembabe almost 16 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)

mgravell almost 16 years ago