首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >关于Protobuf-Net在C#中的使用

关于Protobuf-Net在C#中的使用
EN

Stack Overflow用户
提问于 2011-12-05 20:12:14
回答 1查看 172关注 0票数 2

我开始在C#和WPF项目中使用Protobuf-Net。我有一个看起来像这样的类:

类研究-包含临床查找对象的集合;每个临床查找对象包含屏幕截图对象(屏幕截图类的实例)的集合。

当我序列化研究对象时,临床发现被正确序列化了。但是,包含在每个临床查找对象中的屏幕截图对象的内部集合没有被序列化。

这在二进制格式化程序中可以正常工作。你能给我讲讲吗?

问候

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-05 20:32:38

在这里运行良好(见下文)。我很乐意提供帮助,但您可能希望添加一个可重现的示例,我可以参考一下。

代码语言:javascript
运行
复制
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
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8385114

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档