如何在 C# 中将 .proto 文件解析为 FileDescriptor?

2023-12-26

我的目标与中所述完全相同这个问题在github上 https://github.com/protocolbuffers/protobuf/issues/6853:

如何读取现有的 .proto 文件并从中获取 FileDescriptor

我无法使用建议的“解决方法”,原因有两个:

  • I have "plain" .proto files, i.e.:
    • 它们是文本文件,就像旧的一样地址簿.proto https://github.com/protocolbuffers/protobuf/blob/master/examples/addressbook.proto
    • 他们不是自我描述 https://developers.google.com/protocol-buffers/docs/techniques#self-description
  • 我不想将协议编译器作为外部应用程序调用。

根据马克的说法 https://stackoverflow.com/a/47635433/11398730这是可能的protobuf网络 https://github.com/protobuf-net/protobuf-net图书馆:

如果没有编译的模式,您将需要一个运行时 .proto 解析器。 [...] protobuf-net 包括一个(protobuf-net.Reflection https://www.nuget.org/packages/protobuf-net.Reflection/)

I found Parsers.cs https://github.com/protobuf-net/protobuf-net/blob/main/src/protobuf-net.Reflection/Parsers.cs

谢谢马克,但我该如何使用/执行此操作? 这是正确的切入点吗? 某处有一个最小的工作示例吗?



var set = new FileDescriptorSet();
set.Add("my.proto", true);
set.Process();
  

这就是你所需要的;请注意,如果您想提供实际内容(而不是让库进行文件访问),则有一个可选的TextReader范围。如果您需要进口:


set.AddImportPath(...);
  

一旦你打过电话Process, the .Files应与.MessageTypes每个文件等

更完整的例子:


var http = new HttpClient();
var proto = await http.GetStringAsync(
 "https://raw.githubusercontent.com/protocolbuffers/protobuf/master/examples/addressbook.proto");

var fds = new FileDescriptorSet();
fds.Add("addressbook.proto", true, new StringReader(proto));
fds.Process();
var errors = fds.GetErrors();
Console.WriteLine($"Errors: {errors.Length}");

foreach(var file in fds.Files)
{
    Console.WriteLine();
    Console.WriteLine(file.Name);

    foreach (var topLevelMessage in file.MessageTypes)
    {
        Console.WriteLine($"{topLevelMessage.Name} has {topLevelMessage.Fields.Count} fields");
    }
}
  

哪个输出:


addressbook.proto
Person has 5 fields
AddressBook has 1 fields

google/protobuf/timestamp.proto
Timestamp has 2 fields
  

请注意,您不必提供timestamp.proto或其导入路径 - 该库嵌入了许多常见的导入,并使其自动可用。

(每个文件是一个FileDescriptorProto;逻辑解析操作中的文件组是FileDescriptorSet- 这是使用的根对象描述符.proto https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/descriptor.proto;请注意,如果您需要编译/二进制模式,则此图中的所有对象也是 protobuf 可序列化的)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 C# 中将 .proto 文件解析为 FileDescriptor? 的相关文章

随机推荐

  • 有没有更好的方法来比较字典值

    我目前正在使用以下函数来比较字典值并显示所有不匹配的值 有没有更快或更好的方法来做到这一点 match True for keys in dict1 if dict1 keys dict2 keys match False print ke
  • Tensorflow - h5模型到tflite转换错误

    我使用预训练的 InceptionV3 模型进行了学习迁移 并保存了 h5 模型文件 之后 我就能做出预测 现在 我想使用 TFLiteConverter convert 方法将 h5 模型转换为 tflite 文件 如下所示 conver
  • 无法在 R 上安装包

    当我尝试为 R 安装 Bioconductor 时遇到问题 我到处搜索解决方案 但没有任何东西真正适合我 请问 有什么建议吗 The downloaded binary packages are in var folders 74 y92t
  • 添加任务计划程序

    我正在开发 Apache Cordova 应用程序 我想知道是否可以将任务添加到调度程序设备 我必须在特定时间运行警报和通知 我该怎么做 我考虑过向调度程序设备添加一个任务并委派责任 这个有可能 至少安卓4 4 Thanks 首先 是的 你
  • 如何使用Rubberduck管理本地git存储库

    我在用着橡皮鸭 http rubberduckvba com 版本 2 0 13 与本地 git 存储库结合使用 用于对我的 VBA 项目进行版本控制 目前 每次我使用 Rubberduck 启动时 我都需要重新打开存储库Manage gt
  • SWIG 接口通过函数参数接收 Java 中的不透明结构引用

    我正在尝试使用 SWIG 来使用适用于 Android 的 Spotify API libspotify https developer spotify com technologies libspotify https developer
  • 标记水平 EMA 线(变量)

    这是我使用 atm 的代码 感谢比约恩 米斯蒂安 len1 input 10 minval 1 title Length len2 input 21 minval 1 title Length len3 input 55 minval 1
  • 创建父子数组 PHP

    我有一个平面数组 我试图将其转换为父子数组 Array 0 gt Array parent id gt t3 42yrg7 id gt t1 cze7b2e 1 gt Array parent id gt t3 42yrg7 id gt t
  • 在 React 中自动缩放输入值的宽度

    我想要一个宽度适应其内容的输入 我正在尝试实施这个类似问题的答案 https stackoverflow com a 38867270 5308892 但是使用 React import React useState from react
  • 将 pageToken 与 Google Analytics Reporting API v4 和 Python 结合使用

    我遵循了有关如何使用 GA Reporting API 通过 Python 从 Google Analytics 下载数据的教程 尽管达到了行数限制 但我仍然能够查询所需的数据 我在文档中看到有一个 pageToken 可以避免这个问题 我
  • git 克隆失败并显示“--stdin 需要 git 存储库”

    我对这个问题感到困惑 并被困在这里 不知道为什么会发生 我正在使用 git clone 从 bitbucket 获取我的存储库 例如 git clone 电子邮件受保护 cdn cgi l email protection mycompny
  • ANTLR 4 和 AST 访客

    我正在尝试将 AST 与 ANTLR4 一起使用 并使用以下文件 生成器 java import org antlr v4 runtime ANTLRInputStream import org antlr v4 runtime CharS
  • 动态更改 Angular 指令元素属性

    我正在尝试创建一个自定义指令来扩展现有元素的功能 我想检测某个属性是否存在 如果不存在则添加它 例如 ng class 我试图在预编译期间实现这一点 但 Angular 不会对新属性的添加做出反应 I 创建了一个笨蛋 http plnkr
  • 为什么 swiftUI 列表不是惰性的(与lazyVStack相比)?

    SwiftUI 中的列表真的很懒吗 我有一个从 coreData 获取的 5000 个元素的列表 我想将它们显示在列表中 我读了很多评论 比如 https developer apple com forums thread 651256 h
  • 如何在 R 中强制 For 循环() 或 lapply() 运行并显示错误消息

    在此代码中 当我使用 for 循环或函数 lapply 时 出现以下错误 Error in get entrypoint debug port Cannot connect R to Chrome Please retry library
  • PowerShell 的特殊数组头/尾分配记录在哪里?

    我刚刚注意到这是有效的 PowerShell 代码 PS gt first rest 1 2 3 该语句将数组中的第一项放入 first以及剩余的项目 rest PS gt first 1 PS gt rest 2 3 它甚至适用于任意数量
  • 将自定义字体添加到 Windows Phone 8 应用程序

    我读过这样的文章Windows Phone 31 天 24 嵌入字体 http www jeffblankenburg com 2010 10 24 31 days of windows phone day 24 embedding fon
  • android 添加 java.lang.management API

    现在我遇到了 Android 不支持的库的问题 事实上它不支持这个API java lang management Eclipse 向我显示了这个错误 10 25 17 53 03 460 java lang NoClassDefFound
  • Android:无法解析符号“编译”

    我在 android studios 添加新向导的帮助下添加了一个新活动 设置 在那之后我得到了这个 错误 47 评估项目 AppName 时出现问题 在 org gradle api internal artifacts dsl depe
  • 如何在 C# 中将 .proto 文件解析为 FileDescriptor?

    我的目标与中所述完全相同这个问题在github上 https github com protocolbuffers protobuf issues 6853 如何读取现有的 proto 文件并从中获取 FileDescriptor 我无法使