protobuf.net 中的子类化
我有一个围绕 protobuf.net 构建的系统,该系统公开了一个我希望最终用户实现的抽象类(foo)。抽象类可通过 protobuf.net 序列化。目前,当我尝试序列化 foo 的实现时,出现错误:
期间发现意外类型 序列化;必须包含类型 与 ProtoIncludeAttribute ;成立 bar 作为 foo 传递
这是有道理的,我还没有告诉系统有关 bar 的信息,所以当我将 bar 作为 foo 传递时,它会感到困惑。有没有一种巧妙的方法来设置,以便程序员使用我的库做事情变得简单(最好只是将字段标记为可序列化,就像正常的 protobuf.net 使用一样?
编辑:显然,我不能使用 protoinclude,因为这需要修改基础库的源代码。
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: Obviously, I cannot use protoinclude, as that requires modifying the source code of the base library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 v1 中,必须装饰基地才能了解孩子们。在 v2 中,此限制被删除;您可以在运行时创建模型并定义您想要的一切。它仍然可以读取属性,这都是并行的(如果您愿意,您可以对不同类型使用不同的方法)。
但是,如果调用者不想知道任何血腥细节,您可以选择将 RuntimeTypeModel 详细信息隐藏在您自己的 API 后面。
v2 可以从主干构建,并且非常稳定 - 不过还有一些 TODO 项目 - 主要是需要完成才能完全兼容的边缘情况。大多数人不会看到这些案例。
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.