Stephen James
Stephen James

Reputation: 73

Using protobuf net with DictionaryBase

I'm new to using protobuf and have an issue trying to serialize a class that inherits from Dictionary. Additional properties are not being serialized. As an examle I have this class

[ProtoContract]
  public class InheritDictionary: Dictionary<string,string>
  {
    private int _myInt;


    [ProtoMember(1)]
    public int MyInt
    {
      get
      {
        return _myInt;
      }
      set
      {
        _myInt = value;
      }
    }

  }

When I serialize the MyInt property is not included. Am I missing something?

Upvotes: 2

Views: 116

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062975

The protobuf specification has no notion of collection objects. On the wire you only get the contained elements - the collection does not feature at all in the data. Because of this, there is nowhere to store any properties of something that is a list - and dictionaries are essentially lists (of key/value pairs).

At the DTO layer, I would say: don't inherit collections. Encapsulate collections instead. Have an object that has a dictionary and has the extra properties.

Upvotes: 1

Related Questions