继承和已知类型问题

发布于 2024-12-13 15:35:59 字数 280 浏览 0 评论 0原文

我正在尝试使用 protobuf .net 序列化从基类继承的类的实例。当尝试序列化\反序列化时,我收到有关“类型不是预期的”的异常。正如我已经读到的,将带有派生类型的 protoinclude 添加到基础中将解决问题。但是,我的问题是我无法添加该属性,因为我不知道派生我的基础的类型。正如我已经读到的,我知道 V2 将提供一种定义类型模型的方法来描述继承。所以我的问题是:

  1. V2 版本什么时候发布?
  2. 同时有解决方法来解决这个问题吗?

预先感谢,

吉尔

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:

  1. When V2 release will be avilable?
  2. Is there a workaround in the mean time to solve this issue?

Thanks in advance,

Gil

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2024-12-20 15:35:59

V2 可以从 nuget 和 google-code 获得(nuget 领先 IIRC 几转 - 两者都落后一点)。有一些 v1 功能尚未重新实现,但对于大多数用途,您不会注意到这一点。它们将在适当的时候重新添加。

重新解决你的问题;这可以通过 MetaType.AddSubType 获得 - 但是,要使用该方法,您必须能够在将来为每个子类型重现相同的 int 标识符 - 它们是重要的键,不得更改。

RuntimeTypeModel.Default[typeof(SomeType)].AddSubType(7, subType);

“对象”属性上还可以使用 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.

RuntimeTypeModel.Default[typeof(SomeType)].AddSubType(7, subType);

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文