ProtoBuf-Net ProtoInclude Generic Type Subclass

前端 未结 2 1683
迷失自我
迷失自我 2020-12-19 12:07

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:

No         


        
2条回答
  •  春和景丽
    2020-12-19 12:21

    You're probably seeing:

    A type can only participate in one inheritance hierarchy

    at the moment, right?

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

    Firstly, Node needs to know separately about the two (ResourceNode and ResourceNode), and secondly: we need to tell ResourceNode about ShipResource only, and ResourceNode 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))]
    [ProtoInclude(4, typeof(ResourceNode))]
    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 : Node { }
    

    since those attributes apply to both of ResourceNode and ResourceNode, 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.

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

    RuntimeTypeModel.Default.Add(typeof(ResourceNode), true)
         .AddSubType(1, typeof (ShipResource));
    RuntimeTypeModel.Default.Add(typeof(ResourceNode), 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 : 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

提交回复
热议问题