我开始在C#和WPF项目中使用Protobuf-Net。我有一个看起来像这样的类:
类研究-包含临床查找对象的集合;每个临床查找对象包含屏幕截图对象(屏幕截图类的实例)的集合。
当我序列化研究对象时,临床发现被正确序列化了。但是,包含在每个临床查找对象中的屏幕截图对象的内部集合没有被序列化。
这在二进制格式化程序中可以正常工作。你能给我讲讲吗?
问候
发布于 2011-12-05 20:32:38
在这里运行良好(见下文)。我很乐意提供帮助,但您可能希望添加一个可重现的示例,我可以参考一下。
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
}
}https://stackoverflow.com/questions/8385114
复制相似问题