序列化类型System.Drawing.Rectangle从.Net到C ++使用protobuf

时间:2014-01-25 10:27:35

标签: c# c++ objective-c protobuf-net

如果我在c#中序列化(使用protobuf)一个矩形(System.Drawing.Rectangle),可以在Objective-C或C ++中反序列化它,因为System.Drawing.Rectangle不是像int这样的常见类型。

如果可能,在c ++或Objective-c中获取此矩形的最佳方法是什么?

2 个答案:

答案 0 :(得分:4)

我通过使用自己的类类型修复它,我的意思是将System.Drawing.Rectangle替换为我自己的类,而不是包含相同的属性。

我的意思是如果你想序列化和反序列化你必须使用常见的类型。

答案 1 :(得分:2)

protobuf-net中不直接支持System.Drawing.Rectangle类型(由于一些额外的属性,自动元组代码不会触发),所以我假设你有一个代理到位,或者您手动配置模型 - 例如:

RuntimeTypeModel.Default.Add(typeof(Rectangle),false)
                .Add("X","Y","Width","Height");

在这种情况下,您需要做的是创建匹配的.proto架构。 protobuf-net实际上会帮助你:

var proto = RuntimeTypeModel.Default.GetSchema(typeof(Rectangle));

对我来说(即我使用的配置)生成:

message Rectangle {
   optional int32 X = 1;
   optional int32 Y = 2;
   optional int32 Width = 3;
   optional int32 Height = 4;
}

这应该可以从任何protobuf实现中使用。