简体   繁体   中英

C# Protobuf, submitting generic type information

I want to serialize C# objects using Protobuf, send them via network and deserialize them on the "other" side.

So Client A has eg the following Code:

Serializer.Serialize<ClassA>(stream, instance);

Now, I want to be able to transmit the typeinformation being "ClassA", too. On the receiving side, Client B has to call:

ClassA instance = Serializer.Deserialize<ClassA>(stream);

Is there an elegant way to directly transmit the generic typeinformation? The only other way that I could think of is to define a gigantic enum, containing all types that can be serialized and put this Information together with the actual instance of ClassA in a wrapper class.

Hints or possible solutions are very much appreciated!

Json.Net can serialize the objects into json, and if you give it the correct hints, it will send through the binary information required to deserialize it on the other side.

If you look at the example here: http://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm

They use a List, where the T is the generic parameter. So in your case T would be something else, but the principle is the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM