什么是protobuf网SerializeWithLengthPrefix标签的说法? [英] What is protobuf-net SerializeWithLengthPrefix tag argument for?

查看:668
本文介绍了什么是protobuf网SerializeWithLengthPrefix标签的说法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该方法接受作为最后一个参数一个整数,但我不知道我明白我会用它整整

This method accepts as the last argument an integer, but I'm not sure I understand what I would use it for exactly.

Serializer.SerializeWithLengthPrefix(stream, object, PrefixStyle.Base128, [tag]);

这同样适用于相应的Deserialize方法实现。

The same holds true for the corresponding Deserialize method.

难道只是一个标记消息添加某种对反序列化查询功能,以过滤掉不需要的邮件的方式还是有其他用途?

Is it just a way to tag messages to add some sort of "querying" capabilities on deserialization to filter out unwanted messages or does it have any other usages?

推荐答案

基本上,它是的可以的是(虽然不一定是),使用时要注意的被添加的报文类型,自推定(使用 * WithLengthPrefix 办法时)是有相同的流在多条消息。

Basically, it is an additional marker that can be (although does not have to be) used to note the "type" of the message being added, since the presumption (when using the *WithLengthPrefix approach) is that there are multiple messages in the same stream.

通过被列入,这也意味着整个的复合的流本身一个完全有效的protobuf消息。

By being included, it also means that the entire composite stream is itself an entirely valid protobuf message.

途径使用这样的:


  • 您可以序列化列表<富> ,然后反复反序列化(带长度前缀)个人项目,反之亦然

  • 与异质的对象,你可以使用在 Serializer.NonGeneric API,允许类型分辨率基于标签,即代码相当于如果1,则发票的;如果2再下订单,如果3则跳过它,如果4,则客户,等等 - 这是如果使用的NetworkStream 为消息发送设备这种做法尤其有用(接下来的消息

  • you can serialize a List<Foo>, and then repeatedly deserialize (with-length-prefix) individual Foo items, or vice-versa
  • with a heterogeneous set of objects, you can use the Serializer.NonGeneric API to allow type-resolution based on the tag, i.e. the code equivalent of "if 1 then Invoice; if 2 then Order, if 3 then skip it, if 4 then Customer", etc - this is especially useful if using a NetworkStream as a message-sending device. This approach (using a different tag per type) allows you to read objects off the stream, and deserialize them correctly, without knowing in advance the type of the next message

有可能的,如果你想忽略这一点 - 只要传递零(IIRC),这将节省(典型值)每追加消息一个字节,而是:这意味着该流不再。一个有效的protobuf本身,它仍可读,当然,通过阅读时传递零

It is possible to omit this if you want - just pass zero (IIRC). This will save (typically) a byte per message appended, but: it means that the stream is no longer a valid protobuf itself. It can still be read, of course, by passing zero when reading.

这篇关于什么是protobuf网SerializeWithLengthPrefix标签的说法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆