Search code examples
c#protobuf-netsetterreadonlyprotogen

protobuf-net - generated class from .proto - Is repeated field supposed to be Read Only with no setter?


I am totally confused about this. I have looked around and can't seem to find a direct answer. I have a .proto file that my project, which has all been java, uses to create some messages.

There is a repeated Info field. Which is a type we created. When I generate the C# classes with protogen, this field comes up as read only and has no setter.

I can't fully build the message without this parameter. So my question is. Are repeated fields supposed to be generated like this and I am supposed to be accessing this read only List some other way? Or is this a bug in the generator?

Generated code:

private readonly global::System.Collections.Generic.List<StringMapEntry> _factoryProperty = new global::System.Collections.Generic.List<StringMapEntry>();
[global::ProtoBuf.ProtoMember(2, Name=@"factoryProperty", DataFormat = global::ProtoBuf.DataFormat.Default)]
public global::System.Collections.Generic.List<StringMapEntry> factoryProperty
{
  get { return _factoryProperty; }
}

Proto file section:

repeated StringMapEntry factoryProperty = 2;

I was probably just missing something really obvious. Thanks for any help!


Solution

  • The list is not read only... You just mutate the list it gives you:

    var order = new Order();
    order.Lines.Add( new OrderLine {...} );
    

    It is actually pretty common for sub-collections to be get-only. That doesn't mean you can't change the contents.