继承和已知类型问题
我正在尝试使用 protobuf .net 序列化从基类继承的类的实例。当尝试序列化\反序列化时,我收到有关“类型不是预期的”的异常。正如我已经读到的,将带有派生类型的 protoinclude 添加到基础中将解决问题。但是,我的问题是我无法添加该属性,因为我不知道派生我的基础的类型。正如我已经读到的,我知道 V2 将提供一种定义类型模型的方法来描述继承。所以我的问题是:
- V2 版本什么时候发布?
- 同时有解决方法来解决这个问题吗?
预先感谢,
吉尔
I'm trying to use protobuf .net to serialize an instance of a class which is inherited from a base class. When trying to serialize\deserialize i'm getting an exception about "type not expected". As I already read, adding the protoinclude with the derived type to the base wil solve the problem. BUT, my problem is that I can't add the attribute, since I don't know the types that will derive my base. As I already read, I understand that V2 is going to provide a way to define a type model in order to describe inheritance. So my questions are:
- When V2 release will be avilable?
- Is there a workaround in the mean time to solve this issue?
Thanks in advance,
Gil
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
V2 可以从 nuget 和 google-code 获得(nuget 领先 IIRC 几转 - 两者都落后一点)。有一些 v1 功能尚未重新实现,但对于大多数用途,您不会注意到这一点。它们将在适当的时候重新添加。
重新解决你的问题;这可以通过 MetaType.AddSubType 获得 - 但是,要使用该方法,您必须能够在将来为每个子类型重现相同的 int 标识符 - 它们是重要的键,不得更改。
“对象”属性上还可以使用 DynamicType - 然而这与基于继承的模型有一些问题;我现在会避免这种情况(这也将很快得到纠正)。
V2 is available both from nuget and google-code (nuget is a few revs ahead IIRC - and both are a little behind the head). There are a few v1 features not yet re-implemented, but for most purposes you won't notice this. They will be re-added in due course.
Re your problem; that is available via MetaType.AddSubType - however, to use that approach you must be able to reproduce the same int identifiers per-subtype in the future - they are important keys and must not change.
There is also DynamicType available on "object" properties - however this has a few kinks with inheritance-based models; I would avoid that for now (this too will be rectified shortly).