4

(Havent spotted a question asking this).

Why cant a Dictionary be serialized?

Most resources, websites, blogs etc say that it cannot be serialized. However reading "CLR via C#" 3rd edition, page 664 gives Dictionary as an example of an object graph which can be serialized.

Note that this chapter talks about Binary Serialization. So is it that it can be serialized using the BinaryFormatter but it cannot be XML serialized?

Or is there some difference here that Im missing between an IDictionary and a Dictionary?

To clarify... under what circumstances can a Dictionary be serialized and under what circumstances can it not be serialized.

Thanks.

Remotec
  • 10,304
  • 25
  • 105
  • 147
  • 1
    what do you want to serialize with? different serializers will treat different types differently, so you can serialize a `Dictionary` to XAML, for example, which is XML, but you cannot serialize it with `XmlSerializer`... – madd0 Dec 01 '11 at 14:16
  • 1
    equally, you mention "Binary Serialization"; BinaryFormatter and NetDataContractSerializer (and, for that matter, protobuf-net) are all binary serializers, but may behave differently. "What serializer" is key here. – Marc Gravell Dec 01 '11 at 14:22
  • Can it only be used to serialized certain types then? I understood that the serialization "plumbing" checks if the object derives from IDictionary and then disables serialization for that object. http://stackoverflow.com/questions/1573922/why-cant-a-dictionary-object-be-xmlserialized-in-c (see quotation in 1st answer). – Remotec Dec 01 '11 at 14:23
  • We cannot answer that unless you first define "it". If "it"====XmlSerializer, then: indeed, that will be a problem. – Marc Gravell Dec 01 '11 at 14:24
  • Ok, so its a restriction when serializing to XML? – Remotec Dec 01 '11 at 14:26

2 Answers2

3

I think this is just a limitation of Microsoft's XML serializer (System.Xml.Serialization.XmlSerializer). Most other serializers will support dictionaries.

This is not a general limitation of serialization, not even xml serialization, just a limitation of that implementation. XmlSerializer is pretty weak in general.

CodesInChaos
  • 106,488
  • 23
  • 218
  • 262
0

Here is the reason why it is so:

Objects are reconstructed from the inside out, and calling methods during deserialization can have undesirable side effects, since the methods called might refer to object references that have not been deserialized by the time the call is made. If the class being deserialized implements the IDeserializationCallback, the OnSerialization method will automatically be called when the entire object graph has been deserialized. At this point, all the child objects referenced have been fully restored. A hash table is a typical example of a class that is difficult to deserialize without using the event listener described above. It is easy to retrieve the key/value pairs during deserialization, but adding these objects back to the hash table can cause problems since there is no guarantee that classes that derived from the hash table have been deserialized. Calling methods on a hash table at this stage is therefore not advisable.
Alew
  • 316
  • 4
  • 12