How to deserialize a Dictionary<TKey, TValue> descendant using binary formatter

I just run across the problem of how to deserialize a Dictionary<TKey, TValue> descendant using BinaryFormatter. Serialization is not a problem, just mark the derived class with [Serializable] attribute and there you go. However, when you try deserializing it an exception is rised saying that there is no appropriate constructor for Dictionary<TKey, TValue> even if a parameterless constructor exists (which is usually enough). The trick is that you have to implement a constructor with (SerializationInfo si, StreamingContext sc) parameters (in simple case it has just to call base class' constructor). The reason for this is, that Dictionary class uses custom serialization via ISerializable interface.

Leave a Reply