Dr. Kumara Sanjaya
Dr. Kumara Sanjaya

Reputation: 33

Regarding usage of Protobuf-Net in C#

I started using Protobuf-Net in C# and WPF projects. I have a class that looks like this:

Class Study - Contains a collection of Clinical Finding Objects; each Clinical Finding object contains a collection of screen shot objects (instances of the screen shot class).

When I serialize the Study object - the Clinical Findings are getting serialized correctly. However the inner collection of Screen shot objects contains within each Clinical Finding object is NOT getting serialized.

This works properly with Binary Formatter. Can you please enlighten me?

Regards

Upvotes: 2

Views: 203

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062925

Works fine here (see below). I'm happy to help, but you might want to add a reproducible example I can look at.

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

Upvotes: 1

Related Questions