Protobuf与多层继承
使用Protobuf.net,我有一个 List<Test>,以及从 Test 继承的对象(即 Test2、Test3、Test4)。
测试定义如下:
[ProtoContract, ProtoInclude(1,typeof(Test2)), ProtoInclude(2, typeof(Test3)), ProtoInclude(3, typeof(Test4))]
public class Test
{
}
如果列表中包含 Test 和 Test2,序列化可以正常进行,但如果我添加一个类型为 Test3 的对象(它继承自 Test2),就会出现错误
未预期的子类型:WinFormsApp1.Test3
并且无法序列化。
我的问题是:是否可以让它在多层继承下工作?
解决方案
只指定下一层级,并对每个更深层的继承层级也如此处理。类似如下:
[ProtoContract, ProtoInclude(1, typeof(Test1))]
public class Test
{}
[ProtoContract, ProtoInclude(2, typeof(Test2))]
public class Test1 : Test
{}
[ProtoContract, ProtoInclude(3, typeof(Test3))]
public class Test2 : Test1
{}
[ProtoContract]
public class Test3 : Test2
{}
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。