我开始在C#和WPF项目中使用Protobuf-Net。我有一个看起来像这样的课程:
课堂学习 - 包含一系列临床发现对象;每个Clinical Finding对象都包含一组屏幕截图对象(屏幕截图类的实例)。
当我序列化研究对象时 - 临床结果正在被正确序列化。但是,每个Clinical Finding对象中包含的Screen shot对象的内部集合不会被序列化。
这适用于二进制格式化程序。你能请教导我吗?
此致
答案 0 :(得分:1)
在这里工作正常(见下文)。我很乐意提供帮助,但您可能想要添加一个我可以看到的可重现的示例。
using System.Collections.Generic;
using System.Linq;
using ProtoBuf;
[ProtoContract]
class Study
{
private readonly List<ClinicalFinding> findings
= new List<ClinicalFinding>();
[ProtoMember(1)]
public List<ClinicalFinding> Findings { get { return findings; } }
}
[ProtoContract]
class ClinicalFinding
{
private readonly List<ScreenShot> screenShots = new List<ScreenShot>();
[ProtoMember(1)]
public List<ScreenShot> ScreenShots { get { return screenShots; } }
}
[ProtoContract]
class ScreenShot
{
[ProtoMember(1)]
public byte[] Blob { get; set; }
}
static class Program
{
static void Main()
{
var study = new Study {
Findings = {
new ClinicalFinding {
ScreenShots = {
new ScreenShot {Blob = new byte[] {0x01, 0x02}},
new ScreenShot {Blob = new byte[] {0x03, 0x04, 0x05}},
}
},
new ClinicalFinding {
ScreenShots = {
new ScreenShot {Blob = new byte[] {0x06, 0x07}},
}
}
}
};
// the following does a serialize/deserialize
var clone = Serializer.DeepClone(study);
int sum = clone.Findings.SelectMany(x => x.ScreenShots)
.SelectMany(x => x.Blob).Sum(x => (int) x); // 28, as expected
}
}