I am trying to write a generalized method to iterate through a protobuf message and basically create an object tree.
So far I have
IMessage message = new Message() as IMessage;
foreach (FieldDescriptor field in message.Descriptor.Fields.InFieldNumberOrder())
{
if (field.FieldType == Google.Protobuf.Reflection.FieldType.Message)
{
if (field.IsRepeated)
{
// How to do this?
}
else
{
Children.Add(new MessageNode(field, (IMessage)field.Accessor.GetValue(message)));
}
}
else
{
Children.Add(new FieldNode(field, message));
}
}
Works fine except for repeated fields (and probably the same problem will crop up for Map fields).
If I do field.Accessor.GetValue(message) for the repeated field, I get an object[RepeatedField<T>] with T being some nested message. However, I cannot iterate over something of type object.
I tried casting object to RepeatedField<IMessage> but this just returns 0.
Is there any way to iterate over a repeated field without knowing the type inside? I would just need an IMessage pointer to the objects inside, from there I can work with the Descriptor in IMessage.