从 protobuf 中的 FieldDescriptor 获取消息 [英] Get message from FieldDescriptor in protobuf

查看:121
本文介绍了从 protobuf 中的 FieldDescriptor 获取消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 protobuf (C#) 中,我想打印不同消息和子消息中的所有字段.如何获取消息类型并再次发送到功能(递归走到最低的孩子)?更具体地说:我必须做什么,那个 fieldDescriptor 像消息一样发送?我搜索解决方案,即更改???".

In protobuf (C#) I want to print all fields inside different messages and submessages. How can I get message type and send to function again (recursive walking to lowest child)? More specific: What I must do, that fieldDescriptor is send like a message? I search solution, which is change "???".

private void PrintAllReportableFieldsinMessage(Google.Protobuf.IMessage message)
{
    foreach (var fieldDescriptor in message.Descriptor.Fields.InFieldNumberOrder())
    {
        if (fieldDescriptor.FieldType == Google.Protobuf.Reflection.FieldType.Message)
        {
            PrintAllReportableFieldsinMessage(???); // What can I send here?
        }
        else
        {
            Google.Protobuf.Reflection.FieldOptions options = fieldDescriptor.GetOptions();
            if (options != null && options.GetExtension(HelloworldExtensions.Reportable))
            {
                var fieldValue = fieldDescriptor.Accessor.GetValue(message);
                var fieldName = fieldDescriptor.Name;
                Dispatcher.Invoke(() =>
                {
                    lReadableResult.Content += fieldName + ":" + fieldValue + "|";
                });
            }
        }
    }
}

推荐答案

我找到了解决方案.如果子消息中的任何值未设置,则 HasValue 返回 false.然后我必须创建一个具有所有默认值的新 Imessage.因此,此代码可用于打印所有消息和子消息中的所有字段名称.

I found a solition. HasValue return false, if any values inside submessage are not set. Then I must create a new Imessage which have all default values. So then this code works for printing all field names in all messages and submessages.

private void PrintAllReportableFieldsinMessage(Google.Protobuf.IMessage message)
{
    foreach (var fieldDescriptor in message.Descriptor.Fields.InFieldNumberOrder())
    {
        if (fieldDescriptor.FieldType == Google.Protobuf.Reflection.FieldType.Message)
        {
            if (fieldDescriptor.Accessor.HasValue(message))
            {
                IMessage submessage = fieldDescriptor.Accessor.GetValue(message) as IMessage;
                PrintAllReportableFieldsinMessage(submessage);
            }
            else {
                IMessage cleanSubmessage = fieldDescriptor.MessageType.Parser.ParseFrom(ByteString.Empty);
                PrintAllReportableFieldsinMessage(cleanSubmessage);
            }
        }
        else
        {
            Google.Protobuf.Reflection.FieldOptions options = fieldDescriptor.GetOptions();
            if (options != null && options.GetExtension(HelloworldExtensions.Reportable))
            {
                var fieldValue = fieldDescriptor.Accessor.GetValue(message);
                var fieldName = fieldDescriptor.Name;
                Dispatcher.Invoke(() =>
                {
                    lReadableResult.Content += fieldName + ":" + fieldValue + "|";
                });
            }
        }
    }
}

这篇关于从 protobuf 中的 FieldDescriptor 获取消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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