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!
repeated : this field can be repeated any number of times (including zero) in a well-formed message. The order of the repeated values will be preserved.
Field numbers are an important part of Protobuf. They're used to identify fields in the binary encoded data, which means they can't change from version to version of your service. The advantage is that backward compatibility and forward compatibility are possible.
A . proto file is similar to a JSON file in that it represents structured data, but you can run a compiler on the . proto file to generate code that can read and write the data in the programming language of your choice.
Protocol Buffer (Protobuf) provides two simpler options for dealing with values that might be of more than one type. The Any type can represent any known Protobuf message type. And you can use the oneof keyword to specify that only one of a range of fields can be set in any message.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With