【问题标题】:Regarding usage of Protobuf-Net in C#关于在 C# 中使用 Protobuf-Net
【发布时间】:2011-12-05 12:12:13
【问题描述】:

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

类研究 - 包含临床发现对象的集合;每个 Clinical Finding 对象都包含一组屏幕截图对象(屏幕截图类的实例)。

当我序列化研究对象时 - 临床发现正在正确序列化。但是,每个临床结果对象中包含的屏幕截图对象的内部集合并未被序列化。

这适用于二进制格式化程序。可以请教一下吗?

问候

【问题讨论】:

  • 添加最少的代码来重现您的类结构会很有用

标签: c#-4.0 protobuf-net


【解决方案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
    }
}

【讨论】:

  • Marc,感谢您的回答。由于我是第一次在 Stack Overflow 上发帖...我不明白如何添加代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 1970-01-01
相关资源
最近更新 更多