如何使用 protobuf-net 或其他序列化程序序列化第 3 方类型?

发布于 2024-12-11 12:04:28 字数 318 浏览 0 评论 0原文

我有 List 但 protobuf-net 给我错误,它没有合同。当我没有来源时如何指定合同?它实际上说它无法推断类型,但我认为这是因为我没有使用它的属性,对吗?

默认的二进制序列化程序也会抱怨,因为该类型未标记为可序列化。

编辑:错误消息是:

Type is not expected, and no contract can be inferred: HtmlAgilityPack.HtmlNode

I have List<HtmlAgilityPack.HtmlNode> but protobuf-net gives me error that it doesn't have a contract. How can I specify a contract for it when I don't have the source? It actually said it couldn't infer the type but I assume it's because I didn't use its attibute, right?

The default binary serializer also complains because the type is not marked as serializable.

EDIT: The error message is:

Type is not expected, and no contract can be inferred: HtmlAgilityPack.HtmlNode

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

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

发布评论

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

评论(2

红颜悴 2024-12-18 12:04:28

坦率地说,对于 HTML,我只是存储...html - 它有点预序列化!然而,要回答这个问题:

在 protobuf-net v2 中,您可以在运行时配置 TypeModel,这允许您通过属性和一些其他技巧执行所有操作(在 v2 中,如果没有指定其他内容,属性只是帮助引导模型) 。而且因为您可以在运行时完成所有这些操作,所以您不需要更改类型 - 因此可以将其应用于您无法控制的模型。 默认模型实例是 RuntimeTypeModel.Default,您可以向模型添加类型,并单独配置每个 MetaType(映射到 Type)。这允许您告诉它要应用哪些成员(属性/字段)、子类型、回调等。

如果这变得太复杂,您还可以指定一个“代理”,它允许您配置一个简单的 DTO,并使用标准转换运算符(显式或隐式)在复杂模型和简单 DTO 模型之间进行更改。

作为信息,默认模型的重要性是:这就是Serializer.*使用的。但是,如果您使用 TypeModel 实例执行序列化/反序列化,则可以为相同类型拥有多个不同配置的模型。

我不记得 HTML-agility-pack 的完整细节,但这些是通过 protobuf-net 适用于您的场景的主要选项。

Frankly, in the case of HTML I'd just store... the html - it is kinda pre-serialised! However, to answer the question:

In protobuf-net v2, you can configure a TypeModel at runtime, which allows everything you can do via attributes and a few other tricks (in v2 the attributes just help steer the model if nothing else is specified). And because you can do all this at runtime, you dont need to change the type - and hence can apply it to models outside your control. The default model instance is RuntimeTypeModel.Default, and you can add types to the model, and configure each MetaType individually (which maps to Type). This allows you to tell it what members (properties/fields), sub-types, callbacks, etc to apply.

If that gets too complex, you can also specify a "surrogate", which allows you to configure a simple DTO, and use a standard conversion operator (explicit or implicit) to change between the complex model and the simple DTO model.

For info, the significance of the default model is: that is what Serializer.* uses. However, if you use the TypeModel instance to perform serialization/deserialization you can have multiple differently configured models for the same types.

I can't remember the full details of HTML-agility-pack, but those are the main options available for your scenario via protobuf-net.

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