【发布时间】:2012-11-21 13:20:44
【问题描述】:
我有协议缓冲区格式的数据,可以描述如下
message Sections {
repeated Section sections = 1;
}
message Section {
required uint32 type = 1;
required bytes payload = 2;
}
message SectionType1 {
required int32 fieldType1 = 1;
// ...
}
message SectionType2 {
required int32 fieldType2 = 1;
// ...
}
message SectionType3 {
required int32 fieldType3 = 1;
// ...
}
我正在使用 protobuf-net 库(+ protogen + 预编译)。 如何将此类数据反序列化为类似于
的 DTOpublic class Sections
{
public List<Section> Sections { get; }
}
public abstract class Section
{
}
public class SectionType1 : Section
{
public int FieldType1 { get; }
}
public class SectionType2 : Section
{
public int FieldType2 { get; }
}
public class SectionType3 : Section
{
public int FieldType3 { get; }
}
是否可以使用 .NET 中的此类数据(使用预编译,因为我使用的是轻量级框架)?
【问题讨论】:
-
协议缓冲区用于序列化信息——以扁平和顺序的方式。您应该将在此类事物中使用的任何类型视为“形状”,仅包含数据,不包含继承或行为。请参阅数据传输对象。
标签: .net protocol-buffers protobuf-net