从 C# 读取 protobuf3 自定义选项 [英] Reading a protobuf3 custom option from C#

查看:37
本文介绍了从 C# 读取 protobuf3 自定义选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL;DR

根据文档,如果我使用 C++,我可以使用 string value = MyMessage::descriptor()->options().GetExtension(my_option); 读取自定义选项的值代码>.Java 和 Python 也有类似的例子.但我正在做 C#,我可以找到一个等价的.我可以做吗,如果可以,怎么做?

According to the doc, if I were doing C++, I could read the value of a custom option using string value = MyMessage::descriptor()->options().GetExtension(my_option);. There are similar examples for Java and Python. But I'm doing C# and I could find an equivalent. Can I do it, and if yes, how?

更多详情

我正在操作使用 protobuf3 生成的类.架构声明了一个自定义选项.它看起来像这样:

I'm manipulating classes generated with protobuf3. The schemas are declaring a custom option. It looks like this:

import "google/protobuf/descriptor.proto";

extend google.protobuf.MessageOptions {
  string my_option = 51234;
}

message MyMessage {
  option (my_option) = "Hello world!";
}

我的代码被提供了一个从 MyMessage 生成的对象,我想读取这个选项的值(这里是 Hello world!)

My code is being provided an object generated from MyMessage, and I'd like to read the value of this option (here Hello world!)

更新:我没有使用 protobuf-net.现在 protobuf 原生支持 C#,我正在使用 Google 的 protobuf3 C# 库.

Update: I'm not using protobuf-net. Now that C# is natively supported by protobuf, I'm using Google's protobuf3 C# library.

推荐答案

看起来这个功能还没有实现:https://github.com/google/protobuf/issues/1603

Looks like the feature hasn't been implemented yet: https://github.com/google/protobuf/issues/1603

看起来这只是时间问题,他们对拉取请求持开放态度.因此,根据您需要它的时间,您可能是执行此操作的人:)

It also looks like it's only a matter of time and they're open to pull requests. So depending on how soon you need it, you could be the one doing the implementation :)

这篇关于从 C# 读取 protobuf3 自定义选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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