从C ++中的协议缓冲区获取所有字段名称?

时间:2016-01-27 20:28:50

标签: c++ protocol-buffers

有没有办法在C ++中使用其描述符获取protobuff消息的所有字段?

有一种方法可以在 Python 中这样做: Getting all field names from a protocol buffer?

只是想知道C ++中是否存在相同的东西。试图找到descriptor.h上的任何内容,但没有成功。

1 个答案:

答案 0 :(得分:5)

是。如果您有Descriptor,则会使用Descriptor::field(int index)获取字段数。然后,使用FieldDescriptor::name()迭代字段,返回FieldDescriptor,您可以使用import java.util.*; public class Assignment1 { public static void main(String args[]) { List list = new List(); list.addCustomer("man"); list.addCustomer("man"); //System.out.println(list); list.printx(); } } class Customer{ public String name; public Customer(String name) { name = this.name; } } class List { ArrayList<Customer> list = new ArrayList<Customer>(); public void addCustomer(String name1) { Customer x = new Customer(name1); list.add(x); System.out.println(list.get(0)); } public void printx() { for(int i =0;i < list.size();i++) { System.out.println(list.get(i).name); } } } 找到每个字段的名称。