这几天在用protobuf,遇到一个问题。
我想获取protobuf消息中的所有字段,我知道一个方法,使用field_count()
获取消息的字段计数,然后使用函数FindFieldByNumber()
获取所有字段。但是,如果消息的字段编号是不连续的,例如:
message MyPb
{
uint32 id =1;
int32 score =2;
string name =5;
uint32 high =6;
}
那么,MyPb的字段数是4,我用
for(int i=1; i<=count; ++i)
{
descriptor->FindFieldByNumber(i);
}
其中计数=4。
使用这个方法,我可以得到字段name
和high
吗?
如果没有,有人知道更好的方法吗?
非常感谢。
最佳答案
您可以使用 descriptor->field(i)
代替 FindFieldByNumber()。请查看the documentation for the difference between these two functions .
关于protocol-buffers - 如何获取 protobuf 消息中的所有字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43604128/