简体   繁体   English

C#Protobuf,提交通用类型信息

[英]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. 我想使用Protobuf序列化C#对象,通过网络发送它们,并在“另一端”反序列化它们。

So Client A has eg the following Code: 因此,客户A具有例如以下代码:

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

Now, I want to be able to transmit the typeinformation being "ClassA", too. 现在,我也希望能够传输类型信息为“ ClassA”。 On the receiving side, Client B has to call: 在接收方,客户B必须致电:

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. 我唯一想到的另一种方法是定义一个巨大的枚举,其中包含可以序列化的所有类型,并将此信息与ClassA的实际实例放到包装器类中。

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. Json.Net可以将对象序列化为json,如果您提供正确的提示,它将通过另一端发送反序列化所需的二进制信息。

If you look at the example here: http://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm 如果您在此处查看示例: http : //www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm

They use a List, where the T is the generic parameter. 他们使用列表,其中T是通用参数。 So in your case T would be something else, but the principle is the same. 因此,在您的情况下,T可能是其他东西,但是原理是相同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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