A protobuff network that does not serialize a generic type inherits from a generic type

I have this model:

[ProtoContract]
[ProtoInclude(2, typeof(TestRequest<>))]
public class XClass<T>
{
    public XClass()
    {
    }

    [ProtoMember(1)]
    public T Value { get; set; }
}

[ProtoContract]
public class TestRequest<T>: XClass<T> where T : ConcreteClass
{

    public TestRequest()
    {
    }

}

[ProtoContract]
public class ConcreteClass
{
    [ProtoMember(1)]
    public string Name { get; set; }
}

And if I try to serialize and deserialize with protobuf-net:

TestRequest<ConcreteClass> request = new TestRequest<ConcreteClass>();
request.Value = new ConcreteClass() { Name = "test" };
MemoryStream msTestString = new MemoryStream();
Serializer.Serialize(msTestString, request);
msTestString.Position = 0;
request = Serializer.Deserialize < TestRequest<ConcreteClass>>(msTestString);

And after that, if I check request.Value, it is null.

What am I doing wrong?

+4
source share
2 answers

This is by design with protobuf-net. One of the reasons this is so quick and easy is that he doesn't care about the type of information. This, unfortunately (well, depending on your point of view) completely excludes inheritance with it.

see protobuf with inheritance? for more information

+1
source

protobuf-net, TestRequest<> 2... TestRequest<>: , TestRequest<> protobuf-net.

:

[ProtoContract]
[ProtoInclude(2, typeof(TestRequest<Foo>))]
[ProtoInclude(3, typeof(TestRequest<Bar>))]
[ProtoInclude(4, typeof(TestRequest<Blap>))]

, , , .

, :

[ProtoContract]
[ProtoInclude(2, typeof(TestRequest<T>))]
public class XClass<T> {}

, , , , , . , , , , , - - , , , .

+1

All Articles