简体   繁体   English

是否可以使用Protocol Buffers C#(ProtoBuf-net)序列化复杂对象

[英]Is it possible to serialize complex object with Protocol Buffers C# (ProtoBuf-net)

Is it possible to serialize complex object with Protocol Buffers C# (ProtoBuf-net) without using Protocontract and proto files ? 是否可以在不使用Protocontract和proto文件的情况下使用Protocol Buffers C#(ProtoBuf-net)序列化复杂对象?

[ProtoBuf.ProtoContract(ImplicitFields = ProtoBuf.ImplicitFields.AllPublic)]

I have tried to use the ProtoContract but even then I can't serialize object (It is a LLBLGen ORM object). 我试图使用ProtoContract,但即使这样我也无法序列化对象(它是一个LLBLGen ORM对象)。

Yes; 是; there are various options here; 这里有各种选择;

  • firstly, note that "implicit fields" is brittle if you add members, since it has to make more guesses than I would like; 首先,请注意,如果添加成员,“隐含字段”会很脆弱,因为它必须做出比我想要的更多的猜测; only use that with stable contracts 只能用稳定的合同
  • you can apply a default behaviour globally via GlobalSettings, but I tend to advise against it 您可以通过GlobalSettings 全局应用默认行为,但我倾向于反对它
  • protobuf-net v1 can also work with: protobuf-net v1也适用于:
    • XmlType/XmlElement attribute pairs, as long as the XmlElement specifies an Order XmlType / XmlElement属性对,只要XmlElement指定Order
    • DataContract/DataMember attribute pairs, as long as the DataMember specifies an Order DataContract / DataMember属性对,只要DataMember指定一个Order
    • partial classes; 部分班级; even for properties, via ProtoPartialMember attribute(s), etc 甚至对于属性,通过ProtoPartialMember属性等
  • protobuf-net v2 can be used 100% without attributes of any kind, by using a TypeModel to describe the interesting types at runtime ; 通过使用TypeModel 在运行时描述有趣的类型 protobuf-net v2可以100%使用,没有任何类型的属性; this can also compile the model to a dedicated serialization dll if you need (in particular for use with AOT-dependent devices) 如果需要,这也可以将模型编译为专用的序列化DLL(特别是与AOT相关的设备一起使用)

I can advise more, but there are a number of options presented; 我可以提供更多建议,但有很多选择; tell me which is/are most appropriate and I can add more detail. 告诉我哪个是最合适的,我可以添加更多细节。

Re .proto files; Re .proto文件; those are (and have always been) entirely optional with protobuf-net, as I recognise that there are a lot of cases where a code-first approach (or retrofit of serialization to an existing model) is useful. 那些(并且一直是) 完全可选的protobuf-net,因为我认识到很多情况下代码优先方法(或对现有模型的序列化改造)是有用的。 Three is a code-generator if you choose to use .proto, of course. 如果您选择使用.proto,则三代码生成器。

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

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