开发者

Can protobuf-net be used to (de)serialize a list containing heterogeneous objects?

开发者 https://www.devze.com 2023-03-07 08:12 出处:网络
Is it possible to (de)serialize a list of heterogeneous objec开发者_如何学Cts?A List<object> poses a number of challenges for a serialization format that does not include type metadata... but as

Is it possible to (de)serialize a list of heterogeneous objec开发者_如何学Cts?


A List<object> poses a number of challenges for a serialization format that does not include type metadata... but as always, there are tricks, traps, and workarounds.

If you mean at the outermost level, there are a few options here that revolve around using a different tag(/field-number) to indicate the message type. This is useful on an API (for example, a socket conversation) where different messages might be expected at any time. As example would be: How can I send multiple types of objects across Protobuf?

Inside a message is somewhat trickier; if the list of candidate types is small, then a reasonable workaround is something like presented here: Protobuf attributes with a hierarchy of generic classes, i.e. treating each anticipated type as a specialization.

If the type can't be predicted, then v2 includes another workaround, but including more metadata than normal; see http://marcgravell.blogspot.com/2011/03/objects-graphs-and-all-that-jazz.html

0

精彩评论

暂无评论...
验证码 换一张
取 消