0

I'm trying to serialize a dictionary containing non-primitive values. I'm using the normal approach for serializing a dictionary, by implementing IXmlSerializable. Inside the WriteXML method, I convert my non-primitive value to a string (how this converts is not relevant, but that part is working). Inside ReadXML I convert the deserialized value (string) back to the original type.

However, this approach throws an error: InvalidOperationException: To be XML serializable, types which inherit from IEnumerable must have an implementation of Add(System.Object) at all levels of their inheritance hierarchy.

I assume this is because the value is not primitive. However, it shouldnt need to be serializable as its not serialized (but instead converted to string). Any ideas how to work around this?

Should have mentioned I'm using generics, and the runtime is an Mono environment. The class definition looks like the follwing:

public class SerializableRefDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable where TValue : NonPrimitiveObject
Magnus Andersson
  • 234
  • 1
  • 2
  • 10

1 Answers1

3

It seems type of objects in the Dictionary is not the reason of your issue. If you have class inherited from Dictionary you have to implement in your class

public void Add(object o);

Have you seen this answer?

Community
  • 1
  • 1
Vuder
  • 35
  • 3
  • Thanks for the idea, but that didn't help. From the full error message I get that it's actually trying to serialize the original object type (which is not primitive). – Magnus Andersson Oct 19 '12 at 17:18
  • FYI: the serialization works for a primitive object (without the convert to string code), without implementing the add function. – Magnus Andersson Oct 19 '12 at 17:20