Sunday, October 14, 2007

Serializable dictionary in C#

Did you ever need to serialize a generic key-value dictionary like the Dictionary class? Well in .Net that class is not serializable, in fact the interface IDictionary is not serializable for some unknown reason so all the classes that implement the IDictionary interface (SortedDictionary , Dictionary ) can not be serialized unless you specify some custom code.
The best code I have found online is written by Paul Welter and you can see it here. I needed to implement a SortedDictionary so the only change I made to that code is implement the SortedDictionary instead of the Dictionary.