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>
I’ve been using ProtoInclude on the base Node type for all the normal types.
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.
You’re probably seeing:
at the moment, right?
The issue becomes clearer when you recall that
ResourceNode<T>is not a closed type – butResourceNode<Ship>andResourceNode<SomeType>are. This means 2 things:Firstly,
Nodeneeds to know separately about the two (ResourceNode<Ship>andResourceNode<SomeType>), and secondly: we need to tellResourceNode<Ship>aboutShipResourceonly, andResourceNode<SomeType>aboutSomeResourceonly.The first is easy enough with the attribute approach:
However, the second bit can’t be cleanly expressed in any current release. We can’t currently use:
since those attributes apply to both of
ResourceNode<Ship>andResourceNode<SomeType>, and represent illegal inheritance chains. The duplicated1in 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:
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:
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