(注意:我无法复制“组标签”问题;请参阅编辑历史,了解我对此的最初想法,现在已删除;如果你能帮我复制这个,我会很感激的)
问题是
SubReports
. 你已经定义了这个
二者都
和
[ProtoContract]
); 后者优先,所以它试图序列化
单一的
列表上的子报告(总是
null
?).
如果将此更改为:
// note no attributes, no child property
public class SubReports : List<SubReport> { }
Report.SubReports
A.
List<SubReport>
它应该很好用。以下工作:
static void Main() {
byte[] blob;
// store a report
using (MemoryStream ms = new MemoryStream()) {
Report report = new Report {
SubReports = new List<SubReport> {
new SubReport { Name="Q1"},
new SubReport { Name="Q2"},
new SubReport { Name="Q3"},
new SubReport { Name="Q4"},
}
};
Serializer.Serialize(ms, report);
blob = ms.ToArray();
}
// show the hex
foreach (byte b in blob) { Console.Write(b.ToString("X2")); }
Console.WriteLine();
// reload it
using (MemoryStream ms = new MemoryStream(blob)) {
Report report = Serializer.Deserialize<Report>(ms);
foreach (SubReport sub in report.SubReports) {
Console.WriteLine(sub.Name);
}
}
}
显示blob:
0A040A0251310A040A0251320A040A0251330A040A025134