简体   繁体   English

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

[英]How can I serialize a 3rd party type using protobuf-net or other serializers?

I have List<HtmlAgilityPack.HtmlNode> but protobuf-net gives me error that it doesn't have a contract. 我有List<HtmlAgilityPack.HtmlNode>但是protobuf-net给了我一个错误,它没有合同。 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

Frankly, in the case of HTML I'd just store... the html - it is kinda pre-serialised! 坦率地说,在HTML的情况下,我只是存储... html - 它有点预先序列化! 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). 在protobuf-net v2中,您可以在运行时配置TypeModel,它允许您通过属性和一些其他技巧完成所有操作(在v2中,如果没有指定其他内容,属性只是帮助引导模型)。 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). 默认模型实例是RuntimeTypeModel.Default,您可以向模型添加类型,并单独配置每个MetaType(映射到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. 如果这太复杂,您还可以指定“代理”,它允许您配置简单的DTO,并使用标准转换运算符(显式或隐式)在复杂模型和简单DTO模型之间进行更改。

For info, the significance of the default model is: that is what Serializer.* uses. 有关信息, 默认模型的重要性是:这就是Serializer.*使用的。 However, if you use the TypeModel instance to perform serialization/deserialization you can have multiple differently configured models for the same types. 但是,如果使用TypeModel实例执行序列化/反序列化,则可以为相同类型配置多个不同配置的模型。

I can't remember the full details of HTML-agility-pack, but those are the main options available for your scenario via protobuf-net. 我不记得HTML-agility-pack的全部细节,但这些是通过protobuf-net为您的场景提供的主要选项。

For BSon you can specify your own serializer for any class; 对于BSon,您可以为任何类指定自己的序列化程序; see http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-Writeacustomserializer 请参阅http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-Writeacustomserializer

Here's an example using it to serialize C# dynamic variables. 这是一个使用它来序列化C#动态变量的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM