ProtoBuf-Net ProtoInclude 泛型类型子类 [英] ProtoBuf-Net ProtoInclude Generic Type Subclass

查看:23
本文介绍了ProtoBuf-Net ProtoInclude 泛型类型子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用从泛型类继承的对象的子类的 ProtoBuf-Net 时遇到了一些问题.

I'm having some problems with ProtoBuf-Net with a subclass of an object which inherits from a generic class.

我的继承树看起来像这样:

My inheritance tree looks like this:

Node
    SomeNodeType
    SomeOtherType
    ResourceNode<T>
        ShipResource : ResourceNode<Ship>
        SomeResource : ResourceNode<SomeType>

我一直在所有普通类型的基本 Node 类型上使用 ProtoInclude.

I've been using ProtoInclude on the base Node type for all the normal types.

使用 protobuf-net 实现这种层次结构的最佳方法是什么?我试过只包含所有内容,但出现错误,这似乎源于 protobuf 试图将对象反序列化为它的父对象之一.

What would be the best way of achieving this hierarchy with protobuf-net? I've tried just including everything, but I get errors which seem to stem from protobuf trying to deserialise the object as one of it's parent objects.

推荐答案

您可能会看到:

一个类型只能参与一个继承层次

A type can only participate in one inheritance hierarchy

目前,对吗?

当您回忆起 ResourceNode 不是封闭类型 - 但 ResourceNodeResourceNode 时,问题变得更加清晰代码> .这意味着两件事:

The issue becomes clearer when you recall that ResourceNode<T> is not a closed type - but ResourceNode<Ship> and ResourceNode<SomeType> are. This means 2 things:

首先,Node 需要分别了解两者(ResourceNodeResourceNode),其次:我们需要告诉ResourceNode关于ShipResourceonly,以及ResourceNode关于SomeResource .

Firstly, Node needs to know separately about the two (ResourceNode<Ship> and ResourceNode<SomeType>), and secondly: we need to tell ResourceNode<Ship> about ShipResource only, and ResourceNode<SomeType> about SomeResource only.

第一个很容易使用属性方法:

The first is easy enough with the attribute approach:

[ProtoContract]
[ProtoInclude(1, typeof(SomeNodeType)), ProtoInclude(2, typeof(SomeOtherType))]
[ProtoInclude(3, typeof(ResourceNode<Ship>))]
[ProtoInclude(4, typeof(ResourceNode<SomeType>))]
public class Node { }

然而,第二个位不能在任何当前版本中清晰表达.我们目前无法使用:

However, the second bit can't be cleanly expressed in any current release. We can't currently use:

[ProtoContract]
[ProtoInclude(1, typeof(ShipResource)), ProtoInclude(1, typeof(SomeResource))]
public class ResourceNode<T> : Node { }

因为这些属性适用于ResourceNodeResourceNode,并代表非法的继承链.上面重复的1是故意的,因为它们冲突,同样是因为它们是并行分支.

since those attributes apply to both of ResourceNode<Ship> and ResourceNode<SomeType>, and represent illegal inheritance chains. The duplicated 1 in the above is intentional, as they are not in conflict, again because they are parallel branches.

在 v2 中,我们可以做的是明确配置这种关系:

What we can do, in v2, is configure this relationship explicitly:

RuntimeTypeModel.Default.Add(typeof(ResourceNode<Ship>), true)
     .AddSubType(1, typeof (ShipResource));
RuntimeTypeModel.Default.Add(typeof(ResourceNode<SomeType>), true)
     .AddSubType(1, typeof(SomeResource));

想要做的是调整解析器,使其能够将其检测为常见情况,以便您可以简单地使用属性:

What I want to do is tweak the resolver such that it is able to detect this as a common-case, so that you can simply use the attributes:

[ProtoContract]
[ProtoInclude(1, typeof(ShipResource)), ProtoInclude(1, typeof(SomeResource))]
public class ResourceNode<T> : Node { }

我添加了一个待办事项"项目,但测试失败.然而,有趣的是,在设置它时,我也发现了一个不愉快的场景,所以我需要先解决这个问题

I have added a "todo" item and failing test for this. However, interestingly while setting that up I also found a scenario where something isn't playing happily, so I'll need to fix that first

这篇关于ProtoBuf-Net ProtoInclude 泛型类型子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆