Is it possible to serialize complex object with Protocol Buffers C# (ProtoBuf-net)
Is it possible to serialize comp开发者_高级运维lex object with Protocol Buffers C# (ProtoBuf-net) without using Protocontract and proto files ?
[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).
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
- protobuf-net v1 can also work with:
- XmlType/XmlElement attribute pairs, as long as the XmlElement specifies an Order
- DataContract/DataMember attribute pairs, as long as the DataMember specifies an Order
- partial classes; even for properties, via ProtoPartialMember attribute(s), etc
- protobuf-net v2 can be used 100% without attributes of any kind, by using a TypeModel to describe the interesting types at runtime; this can also compile the model to a dedicated serialization dll if you need (in particular for use with AOT-dependent devices)
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; 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. Three is a code-generator if you choose to use .proto, of course.
精彩评论