Examining a protobuf message - how to get field values by name?

南楼画角 提交于 2019-12-05 00:54:49

I am not sure you are looking for Descriptors#findFieldByName(name). You can try with followings:

FieldDescriptor fieldDescriptor = message.getDescriptorForType().findFieldByName("fieldXyz");
Object value = message.getField(fieldDescriptor);

I know this is tagged for java but incase anyone is looking for a way to get the value in c++: (Assuming: field = FieldDescriptor* which contains a int32)

    int32_t value = message_1.GetReflection()->GetInt32(message_1, field);

It took me a while to get this and didn't find any stackoverflow references hence adding it. Hope it helps. Thanks!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!