You cannot use the default XmlSerializer
implementation to (de)serialize IDictionary
objects to Xml. From the MSDN XmlSerializer
documentation:
The XmlSerializer gives special
treatment to classes that implement
IEnumerable or ICollection. A class
that implements IEnumerable must
implement a public Add method that
takes a single parameter. The Add
method's parameter must be of the same
type as is returned from the Current
property on the value returned from
GetEnumerator, or one of that type's
bases.
The Dictionary<>
class implements both interfaces, but does not have an Add
method that takes a single parameter, so it fails.
More to the point, the xml serialization code explicitly disallows classes that implement IDictionary
(from Reflector, looking at TypeScope.GetDefaultIndexer
):
if (typeof(IDictionary).IsAssignableFrom(type)) {
if (memberInfo == null) {
throw new NotSupportedException(...);
}
throw new NotSupportedException(...);
}
So you can't even make the work by subclassing Dictionary<>
and adding the required Add
overload.
The article Generic Dictionaries vs. the XmlSerializer describes one workaround:
- First, decorate the Dictionary property with
an XmlIgnore attribute. This tells the
XmlSerializer to ignore that property,
eliminating the reflection error.
- Create a new public property that takes and returns an array of
DictionaryEntry objects. I tend to
name these specially
(_x_DictionaryName) so that it is
clear one should not generally use
this property.
- Serialize away.