2011-12-05 84 views
2

我開始在C#和WPF項目中使用Protobuf-Net。我有一個看起來像這樣的課程:關於在C#中使用Protobuf-Net

課程研究 - 包含臨牀查找對象的集合;每個臨牀查找對象都包含一組屏幕截圖對象(屏幕截圖類的實例)。

當我序列化研究對象 - 臨牀研究結果正確序列化。但是,每個臨牀查找對象中的屏幕截圖對象的內部集合都沒有被序列化。

這適用於二進制格式化程序。你能開導我嗎?

Regards

+0

這將是添加的最起碼的代碼,重現你的類結構 – CharlesB

回答

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 
    } 
} 
+0

馬克有用的,感謝我張貼首次對堆棧溢出的answer.Since ......我不明白我怎麼會添加代碼。 –