简体   繁体   English

使用知道架构的 protobuf-net 反序列化未知的 object

[英]Deserialize unknown object with protobuf-net knowing the schema

I have to parse back message of unknown type, serialized by protobuf-net.我必须解析由 protobuf-net 序列化的未知类型的返回消息。

I have succesfully parsed back FileDescriptorSet from raw bytes, I mean I know type name, it's fields etc. But how to deserialize message back withouth knowing it's type?我已经成功地从原始字节解析回 FileDescriptorSet,我的意思是我知道类型名称,它的字段等。但是如何在不知道它的类型的情况下反序列化消息? This question and Marc's answer is close, but I fail to understand how to use Extensible class. 这个问题和 Marc 的回答很接近,但我不明白如何使用 Extensible class。

You might need to create a non-abstract class to play with, but class Foo: Extensible {} should suffice, then Serializer.Deserialize(source).您可能需要创建一个非抽象的 class 来玩,但 class Foo: Extensible {} 就足够了,然后是 Serializer.Deserialize(source)。

So I dont have "Foo", I have only FileDescriptorSet and serialized bytes of message.所以我没有“Foo”,我只有 FileDescriptorSet 和序列化的消息字节。 I need to do it in code "on-the-fly".我需要在“即时”代码中完成。 How to marry FileDescriptorSet with byte array, containing values of unknown message type?如何将 FileDescriptorSet 与字节数组结合,包含未知消息类型的值?

There is no simple answer to this question, and I haven't yet had need to fully implement an example.这个问题没有简单的答案,我还不需要完全实现一个例子。 The only thing Extensible does for us here is allow basic access to ad-hoc fields still stored as bytes. Extensible在这里为我们做的唯一一件事是允许对仍以字节形式存储的临时字段进行基本访问。 So: you can deserialize an Extensible from your data source, and then use Extensible.GetValue<Type>(obj, fieldNumber) and the associated overloads of that API.因此:您可以从数据源反序列化Extensible ,然后使用Extensible.GetValue<Type>(obj, fieldNumber)和 API 的相关重载。 You would need to do this based on your own interpretation of the DescriptorProto that you're looking at (a DescriptorProto is a type inside a FileDescriptorSet ).您需要根据自己对正在查看的DescriptorProto的解释来执行此操作( DescriptorProtoFileDescriptorSet中的一种类型)。 For example, you might enumerate .Fields , and for each: infer some <T> from the .type and .TypeName (which you would have to resolve to another DescriptorProto or EnumDescriptorProto etc), and the .Number (for the tag).例如,您可以枚举.Fields ,并且对于每个:从.type.TypeName (您必须将其解析为另一个DescriptorProtoEnumDescriptorProto等)和.Number (用于标签)推断出一些<T> For any message type, you can just use Extensible again and repeat the process recursively.对于任何消息类型,您都可以再次使用Extensible并递归地重复该过程。 You'd also need to check the .Label to see if it is repeated, and if so: use Extensible.GetValues<T> instead.您还需要检查.Label以查看是否重复,如果重复:请改用Extensible.GetValues<T>

That's the theory.这就是理论。 I haven't had the need to put it into practice.我没有把它付诸实践的必要。

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

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