Arpit Gupta
Arpit Gupta

Reputation: 1277

Does protobuf-net supports HybridDictionary?

I have a HybridDictionary object. Is it possible to use Protobuf-net to serialize HybridDictionary object?

Upvotes: 1

Views: 37

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062975

Not presently, and for good reasons:

  • there is no way of understanding the data-contract of a HybridDictionary, since it only advertises object for both the key and value; protobuf-net really, really wants to understand the types involved
  • it is not appealing to support it because in many cases it would require boxing - typically of the key (if not a string), but often of the value

As such, I would strongly recommend using Dictionary<TKey, TValue> or something that implements IDictionary<TKey, TValue>. In theory we could add APIs allowing you to tell it how to interpret the key and value (and obviously do the work to support that in the library), but I'm dubious as to whether that would actually be of real benefit. Frankly, I'm not sure that there is much purpose in using HybridDictionary these days (or for the last many years).


Actually, until 2.3.0, protobuf-net didn't really support Dictionary<TKey, TValue> directly - at least, not in the way you might think: until the map<...,...> feature was fully implemented, what it actually saw was a list-like-thing, i.e. something that could be interpreted as a sequence of {some type}, with an Add({some type}) method. In the case of dictionary, the {some type} happens to be KeyValuePair<TKey, TValue>, which in turn can be interpreted as a tuple-like type (with members that exactly match the constructor, so infer the contract based on the constructor arguments). Conveniently, this turns out to be the exact same shape that map<...,...> uses, so this approach is interchangeable (data-wise) with the new approach preferred in 2.3.0 (which is discussed more here).

Upvotes: 1

Related Questions