How to serialize interface elements with protobuf.NET?

The following test fails with this error:

"System.InvalidOperationException: There is no suitable IB encoding by default."

[ProtoContract]
public class A
{
    [ProtoMember(1)]
    public IB B { get; set; }
}

public interface IB
{
}

[ProtoContract]
public class B : IB
{
    [ProtoMember(1)]
    public int SomeProperty { get; set; }
}

[TestFixture]
public class TestFixture
{
    [Test]
    public void Test()
    {
        var a = new A {B = new B()};
        using (var m = new MemoryStream())
        {
            Serializer.Serialize(m, a);
        }
    }
}

I am using this implementation of Protobuf.net:

http://code.google.com/p/protobuf-net/

Did I miss something? thank you very much.

+3
source share
1 answer

This is a common function of contract based serializers, including XmlSerializeretc. (i.e., those that do not include type metadata for each object).

There are several things that make this difficult:

  • during deserialization, what type will be created for A.B?
  • , ", ",
    • , , .

, -, "v2" (, , ); :

  • A.B (.. A A.B),
  • - ;
  • , ()

, , , - [ProtoInclude] .

, - . .

+1

Source: https://habr.com/ru/post/1751392/


All Articles