Here is my protobuf's data model:
message GetNewsRespone{
repeated google.protobuf.Any instrument = 1;
}
message Issue{
int64 id = 1;
string title = 2;
}
And here is my attempt to fill that with data:
GetNewsRespone res = new GetNewsRespone();
Issue issue = new Issue();
issue.id = 123;
layer.instrument .AddRange(???);
How can I add anIssue
to my GetNewsRespone.instrument
, which is an Any Array?
This is protobuf-c, a C implementation of the Google Protocol Buffers data serialization format. It includes libprotobuf-c , a pure C library that implements protobuf encoding and decoding, and protoc-c , a code generator that converts Protocol Buffer . proto files to C descriptor code.
Protocol Buffers (Protobuf) is a free and open-source cross-platform data format used to serialize structured data. It is useful in developing programs to communicate with each other over a network or for storing data.
generate the necessary stubs in your chosen platform from the schema (https://protogen.marcgravell.com/ may be useful here) then: use the protbuf library's "deserialize" API for your chosen platform to load the data into an object model. finally: inspect the object model, now populated with the data.
To use the Any type, you must import the google/protobuf/any. proto definition. In the C# code, the Any class provides methods for setting the field, extracting the message, and checking the type. Protobuf's internal reflection code uses the Descriptor static field on each generated type to resolve Any field types.
Use Google.Protobuf.WellKnownTypes.Any.Pack
like:
var myValue = Google.Protobuf.WellKnownTypes.Any.Pack(new Stock());
for your code:
GetNewsRespone res = new GetNewsRespone();
Issue issue = new Issue();
issue.id = 123;
res.instrument = Google.Protobuf.WellKnownTypes.Any.Pack(issue);
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