1
votes

I'm trying to serialize an existing class which inherits from List<> and has members.

When I serialize it with protobuf-net it default to ListHandling which serializes the elements in the list fine but ignores the members on the list itself. If I use the "IgnoreListHandling=true" with the ProtoContract attribute, the inverse happens: members on the list are serialized but list items are lost.

I'm moving an existing application to protobuf so redesigning these kind of classes would be a last resort.

1

1 Answers

2
votes

In common with several other serializers (including core .NET serializers like XmlSerializer), to protobuf-net, something is either a list xor a leaf. Members of a list are never considered: they only exist to act as a list. The IgnoreListHandling switches between the two modes, but the "both a list and a leaf" scenario is not supported.

Frankly, what you describe sounds like a dubious design choice anyway. Normally you would encapsulate a list, not subclass it. So rather than:

class MyStuff : List<Foo> {
    public int ExtraValue {get;set;}
}

I would strongly advocate that this should be implemented:

class MyStuff {
    private readonly List<Foo> foos = new List<Foo>();
    public List<Foo> Foos { get { return foos; } }
    public int ExtraValue {get;set;}
}