简体   繁体   English

C#使用.proto文件反序列化为动态对象

[英]c# Using .proto file to deserialize into a dynamic object

Are there any examples of using an existing .proto file (generated from ProtoBuf.Serializer.GetProto();) and raw bytes to de-serialize into a dynamic object or plain object. 是否有使用现有.proto文件(从ProtoBuf.Serializer.GetProto();生成的文件)和原始字节反序列化为动态对象或纯对象的示例。

Let me stress this is a problem I am tackling to improve the debugging tools I have for my out of memory cache - the objective is to be able to make any object within human readable without having to use the app (as I will store the .proto for each type in the cache as it existed when it was serialization occurred). 让我强调一下,这是我要改进的内存不足缓存调试工具的问题-目的是能够使人类中的任何对象都可读而不需要使用应用程序(因为我将存储。序列化发生时针对缓存中每种类型的原型)。

There are no performance considerations in this solution, all options are on the table. 该解决方案没有性能方面的考虑,所有选项都在表中。 I would be happy if there was a method to do this in protobuf-net, but if I needed to use code generation / alternative libraries, that is just fine. 如果在protobuf-net中有一种方法可以做到这一点,我会感到很高兴,但是如果我需要使用代码生成/替代库,那很好。 I am unsure of what the most prudent approach would be. 我不确定最谨慎的方法是什么。

Protobuf-net itself doesn't have any .proto parsing tools, however one of the companion tools included with the google-code download (but not the NuGet download) is "ProtoGen", which has some support for this. Protobuf-net本身没有任何.proto解析工具,但是Google代码下载(而非NuGet下载)随附的一个辅助工具是“ ProtoGen”,对此有一些支持。 It can be referenced (it is just a .net assembly) and used - that is actually how the integration tests for it operate. 可以引用它(它只是一个.net程序集)并使用-这实际上是对其进行集成测试的方式。 This tool works as ac# code generator; 此工具可作为ac#代码生成器; hook that to CSharpCodeProvider and you have a viable, if basic, mechanism to process data in memory. 将其挂接到CSharpCodeProvider上,您将拥有可行的(如果是基本的)机制来处理内存中的数据。 You wouldn't want to process vast quantities of .proto in a single app-domain this way, though - the runtime isn't good at unloading assemblies. 但是,您不希望以这种方式在单个应用程序域中处理大量.proto -运行时不利于卸载程序集。

To do what you want, it would be preferable to use tools that are specifically designed for that scenario. 要执行您想要的操作,最好使用专门针对该场景设计的工具。 That translates into "somebody wanted that scenario sufficiently that they took the time to investigate, design, implement, test, and support it". 这就意味着“有人想要足够的方案,以至于他们花时间去研究,设计,实施,测试和支持它”。

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

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