C# 4.0 关于Protobuf网在C语言中的使用#
我开始在C#和WPF项目中使用Protobuf Net。我有一个类似这样的类: 课堂研究-包含临床发现对象的集合;每个临床发现对象都包含一组屏幕快照对象(屏幕快照类的实例) 当我序列化研究对象时,临床发现被正确序列化。但是,每个临床发现对象中包含的屏幕快照对象的内部集合未序列化 这适用于二进制格式化程序。你能告诉我吗 “问候”在这里效果很好(见下文)。我很乐意提供帮助,但您可能希望添加一个可复制的示例,我可以看看C# 4.0 关于Protobuf网在C语言中的使用#,c#-4.0,protobuf-net,C# 4.0,Protobuf Net,我开始在C#和WPF项目中使用Protobuf Net。我有一个类似这样的类: 课堂研究-包含临床发现对象的集合;每个临床发现对象都包含一组屏幕快照对象(屏幕快照类的实例) 当我序列化研究对象时,临床发现被正确序列化。但是,每个临床发现对象中包含的屏幕快照对象的内部集合未序列化 这适用于二进制格式化程序。你能告诉我吗 “问候”在这里效果很好(见下文)。我很乐意提供帮助,但您可能希望添加一个可复制的示例,我可以看看 using System.Collections.Generic; using S
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
}
}
使用System.Collections.Generic;
使用System.Linq;
使用ProtoBuf;
[原始合同]
课堂学习
{
私有只读列表结果
=新列表();
[原成员(1)]
公共列表结果{获取{返回结果;}}
}
[原始合同]
类临床发现
{
私有只读列表屏幕截图=新建列表();
[原成员(1)]
公共列表截图{获取{返回截图;}}
}
[原始合同]
课堂截图
{
[原成员(1)]
公共字节[]Blob{get;set;}
}
静态类程序
{
静态void Main()
{
var研究=新研究{
调查结果={
新临床发现{
截图={
新屏幕截图{Blob=新字节[]{0x01,0x02},
新屏幕截图{Blob=新字节[]{0x03,0x04,0x05},
}
},
新临床发现{
截图={
新屏幕截图{Blob=新字节[]{0x06,0x07},
}
}
}
};
//下面执行序列化/反序列化
var clone=Serializer.DeepClone(研究);
int sum=clone.Findings.SelectMany(x=>x.ScreenShots)
.SelectMany(x=>x.Blob).Sum(x=>(int)x);/28,如预期
}
}
添加复制类结构的最简单代码会很有用,谢谢你的回答。因为我是第一次发布堆栈溢出…我不知道如何添加代码。