Serializing a Generic Dictionary

So, I created all this fantastic code (God, I love my work) that used a singleton to hold some data that I needed throughout my app.  One of the properties in that class was a generic dictionary.  Well, as I got further into the application I came across the need to persist this data to an XML file.  Naturally (almost instinctively, which means I have no life), I wrote the few lines of code to use the XMLSerlializer to save this to disk.  The problem is the generic dictionary is not serializable.  I was dreading the idea of changing the way I was storing and using that dictionary all through out my app.

To make a long story short, (too late) I did a search and found this post from Paul Welter’s blog.  All I needed to do was create this class in my project, change the data type on my dictionary in my Singleton and it all worked and serialized perfectly.  Thanks Paul!

Posted in |