Simulate generics with a protocol buffer

I have a class that stores and processes some objects. Depending on the number of inputs, I cannot store objects in memory, so I am trying to serialize my objects to be written to the hard drive using protocol buffers. I am using C # and protobuf-csharp-port . I know protobuf-net as an alternate port; I've worked with the first option so far, but I am open to changes if needed based on my needs.

The class that will be serialized in a simplified form looks like this:

class Entity<T> where T: IComparable<T>
    int id;
    T metaData;


So, at compile time, I don't have any hints about metaData

. Googling I noticed that extensions are the right way to go for tracking (as suggested on the google page and this question ); so I define the file Entity.proto

for the class Entity

like this:

message Entity
    required int32 id = 1 [default = 0];
    extensions 2 to max; 


and I would like the user to provide their own .proto

file for T

without having to access or recompile Entity.proto

. In this regard, my questions:

  • Do I need to change Entity.proto

  • What should it be T.proto

  • How can I access T

    in my C # code?

source to share

1 answer

With this schema, any extensions will be non-generic child values ​​(not subclasses) Entity

. It doesn't seem like generics, but in the end storage (serialization) is often different from implementation ( Entity<T>

etc.). If you can manually set in between: great. But that's not what the library will provide, AFAIK.

For completeness, in terms of protobuf-net: great with Entity<T>

- essentially treats each ( Entity<Foo>

, Entity<Bar>

etc.) as completely separate messages. Protobuf-net is not very motivated by .proto schemas (although a code-generation tool is provided for completeness) - it mainly uses runtime metadata.



All Articles