假设有一个proto message定义如下:
message Example {
optional bool a = 1;
optional bool b = 2;
};
如果想在代码里知道a这个field定义时的给的序号是多少(这里是1):
Example obj;
std::cout << obj.GetDescriptor()->FindFieldByName("a")->number() << std::endl;
// output: 1
如果没有名字为a的字段,obj.GetDescriptor()->FindFieldByName("a")的结果是一个空指针。
如果想知道序号1对应的field的name:
Example obj;
std::cout << obj.GetDescriptor()->FindFieldByNumber(1)->name() << std::endl;
// output: a
如果没有序号为1的字段,obj.GetDescriptor()->FindFieldByNumber(1)的结果是一个空指针。