开发者

Subclassing in protobuf.net

开发者 https://www.devze.com 2023-02-08 17:33 出处:网络
I have a system built around protobuf.net, the system exposes an abstract class (foo) which I expect the end user to implement. The abstract class is serialisable by protobuf.net. Currently, when I tr

I have a system built around protobuf.net, the system exposes an abstract class (foo) which I expect the end user to implement. The abstract class is serialisable by protobuf.net. Currently, when I try to serialise an implementation of foo, I get an error:

Unexpected type found during serialization; types must be included with ProtoIncludeAttribute; found bar passed as foo

This makes sense, I haven't told the system about bar, so when I pass a bar as a foo it gets confused. Is there a neat way to set things up such that it's simple for the programmer using my library to do things (preferably just marking fields as serialisable like normal protobuf.net usage?

Edit开发者_Python百科: Obviously, I cannot use protoinclude, as that requires modifying the source code of the base library.


In v1, the base will have to be decorated to know about the children. In v2 this restriction is removed; you can create a model at runtime and define everything you want. It can still read attributes too, this is all side-by-side (you can use different approaches on different types if you like).

You might, however, choose to hide the RuntimeTypeModel details away behind your own API if the caller doesn't want to know any gory details.

v2 is available to build from the trunk, and pretty much stable - there are some TODO items, though - mainly edge cases that need completing for full compatibility. Most people will not see these cases.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号