Hi I am using the class public class SerializableDictionary : Dictionary, IXmlSerializable in order to serialize a dictionary. However it doesnt work when as values I have collelctions (eg List) or tyes other than the primitive types (eg. int, double, string....). My question is which types the TKey, TValue imply or can hold, and if there is way to have a serializable dictionary which collections as values.
A:
Check out Xml Serializable Generic Dictionary for functionality like this... Each sub-item has to be XmlSerializable.
I know it doesn't answer your question directly, but the MSDN doc is rubbish for that class. So personally I wouldn't use it until its a bit more helpful.
Ian
2010-03-12 10:33:16
Yeah I m already using this piece of code. My problem is that every time I need to check the type of the Value in the dictionary and if I have a value of type List<T> I need to create a XMLSerializer of that type otherwise I create a XMLSerializer of the generic type TValue. So I am wondering if there is a really generic type to hold both collections and T. I read somewhere that I can overload the generic type and define my own generic type such as : MyType, MyType<T>,MyType<T,U> but I m not sure how exactly does it work.
psilos
2010-03-12 11:26:34
If I understand you correctly what you want to do is defined a new IList<T> class with the constraint that T is IXmlSerializable. Then you can implement IXmlSerializable for the IList<T> class too and make it work in a similar way to that dictionary.
Ian
2010-03-12 11:36:06
Well not exactly. I want to implement a serializable dictionary that holds as values both collections(eg. List<T>) and values of type T (eg. double, int, string ...). The version of the serializableDictionary I have now can just holds either the first or the latter one, but not both at the same time. In order to achieve this I need, for each value that i serialize, to check its type and then explicitely instantiate a new XMLserializer of that type. I d rather do that at once instantiating a XMLserializer of generic type "MyType" that can hold both types (collections, and T) if it is possible.
psilos
2010-03-12 13:11:51