显示协议缓冲区消息中的所有元素 [英] Show all elements in a protocol buffer message
问题描述
如何显示协议缓冲区消息中的所有元素?我是否需要使用反射或将消息转换为 XML 消息然后显示它?理想情况下,一些通用代码适用于任何消息.拉斯
How can I show all elements in a protocol buffer message? Do I need to use reflection or convert the message into an XML message and then show it? Ideally some generic code that will work for any message. Lars
推荐答案
protobuf 消息在内部是模棱两可的,除非您有 .proto 架构(或可以推断架构)可用,例如(例如) 一个字符串"线型可以表示:
A protobuf message is internally ambiguous unless you have the .proto schema (or can infer a schema) available, as (for example) a "string" wire-type could represent:
- utf-8 字符串
- 一个 BLOB
- 子消息
- 一个压缩数组
所有连线类型都存在类似的歧义(可能组"除外).
Similar ambiguity exists for all wire-types (except perhaps "groups").
我的建议是在您现有的反序列化过程中运行它(针对您可能在项目中可用的类型模型)以获得适合检查的对象模型.从对象模型中,您有所有常用选项 - 反射、通过 XmlSerializer
/JavaScriptSerializer
等进行序列化
My recommendation would be to run it through your existing deserialization process (against the type-model that you presumably have available in the project) to get an object model suitable for inspection. From the object-model you have all the usual options - reflection, serialization via XmlSerializer
/ JavaScriptSerializer
, etc.
如果你只有原始数据,那么可以使用wireshark插件,或者protobuf-net存在一个ProtoReader类,可能对解析这样的流有用;但这里的重点是,流很难单独破译.
If all you have is the raw data, there is a wireshark plugin that might help, or protobuf-net exists a ProtoReader class that might be useful for parsing such a stream; but the emphasis here is that the stream is tricky to decipher in isolation.
这篇关于显示协议缓冲区消息中的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!