using protobuf-net.dll 2.0.0.431
I'm attempting to serialize a class hierarchy using [DataContract] and [DataMember].
[DataContract]
[KnownType(typeof(LoginRequest))]
public class Message
{
[DataMember(Order = 2)]
public int Id { get; set; }
}
[DataContract]
public class LoginRequest : Message
{
[DataMember(Order = 1)]
public string Username { get; set; }
[DataMember(Order = 2)]
public string Password { get; set; }
}
and to serialize/deserialize:
using (var file = File.Create(filename))
{
Serializer.Serialize(file, loginRequest);
}
LoginRequest deserialized;
using (var file = File.OpenRead(filename))
{
deserialized = Serializer.Deserialize<LoginRequest>(file);
}
ReflectionUtils.Compare(loginRequest, deserialized);
The Id
field of abstract class Message
is not serialized.
To make it work I have to decorate Message with:
[ProtoInclude(1, typeof(LoginRequest))]
Why is this? I've read this similar question but Marc concludes that 'this is no longer required in v2 - you can specify this at runtime, or use DynamicType'
I would prefer not to specify anything extra other than KnownType