Serializable dictionary in C#
Did you ever need to serialize a generic key-value dictionary like the Dictionary
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
3 comments:
"it's not serializable" that's all I needed. Thanks for the quick info :)
You can find another implementation under following link: Serializable dictionary
I've been able to work around this problem. It may not work for all use cases, but will work for most. Instead of using the XmlSerializer (which begins to age in comparison of other .Net serializers), I used XamlServices, here's a bit of code:
var values = new Dictionary();
values.Add("MY KEY", "MY VALUE");
var builder = new StringBuilder();
XamlServices.Save(new StringWriter(builder), values);
var data = builder.ToString();
MessageBox.Show(data);
var result = (IDictionary)XamlServices.Load(new StringReader(data));
MessageBox.Show(result.ToString());
And that worked for me.
Post a Comment