C# 使用protobuf net序列化具有接口类型成员的类

C# 使用protobuf net序列化具有接口类型成员的类,c#,protocol-buffers,protobuf-net,C#,Protocol Buffers,Protobuf Net,我无法使用protobuf net序列化我的类,问题似乎是protobuf net无法序列化接口 interface MyInterface { string name; } [ProtoContract] [ProtoInclude(1, typeof(MyClass1))] [ProtoInclude(2, typeof(MyClass2))] public abstract class ParentClass { [ProtoMember(1)] List<

我无法使用protobuf net序列化我的类,问题似乎是protobuf net无法序列化接口

interface MyInterface
{
    string name;
}

[ProtoContract]
[ProtoInclude(1, typeof(MyClass1))]
[ProtoInclude(2, typeof(MyClass2))]
public abstract class ParentClass
{
    [ProtoMember(1)]
    List<MyInterface> elements;
}


[ProtoContract]
public class MyClass1 : ParentClass, MyInterface
{
    [ProtoMember(1)]
    int x;
}

[ProtoContract]
public class MyClass2 : MyInterface
{
    [ProtoMember(1)]
    string y;
}
接口MyInterface
{
字符串名;
}
[原始合同]
[ProtoInclude(1,typeof(MyClass1))]
[ProtoInclude(2,typeof(MyClass2))]
公共抽象类父类
{
[原成员(1)]
列出要素;
}
[原始合同]
公共类MyClass1:父类,MyInterface
{
[原成员(1)]
int x;
}
[原始合同]
公共类MyClass2:MyInterface
{
[原成员(1)]
弦y;
}
我无法序列化MyClass1类型的任何对象,因为元素是接口列表,可以是Mylass1或MyClass2。我得到一些编码未设置错误


有人能告诉我如何解决这个问题吗。谢谢。

我猜您需要添加:

[ProtoInclude(1, typeof(MyClass1))]
[ProtoInclude(2, typeof(MyClass2))]

您的
MyClass1
MyClass2
,因为您继承自
MyInterface
,序列化将不知道类型。

在当前的官方版本中,我不包括接口序列化支持。然而,我确实有一个补丁(来自另一个用户)似乎能够实现这一点

我还没有将此修补程序应用到核心,只是因为我需要在添加更多功能之前先完成“v2”(特别是当需要为v2完全重新实现该功能时),但是如果您愿意,我很乐意与您共享此修补程序

或者:使用基类而不是接口。这是受支持的(通过
[ProtoInclude]
)-但是,您的
MyClass1
已经有一个父类,这在某种程度上使问题变得复杂



编辑:这现在在v2中受支持。显然,代码必须知道预期的具体实现——但是现在可以将include附加到接口(或者可以选择在普通POCO模型的代码中指定)。

如果您可以共享补丁,那就太好了。。。。我还想知道我是否可以用AppFabric插入protobuf网络,你试过了吗?顺便说一句,这个项目做得很好@ace向我发送电子邮件(请参阅配置文件)以获取修补程序。重新制作ppfabroc,从早期测试版开始就没有了。但您可以始终为其设置blob:)(现在v2支持此功能)