- On deserialization, the constructor that's implicitly a part of ISerializable is invoked at an undefined point in the deserialization of the complete graph. This means that you cannot rely on the value of any referenced objects at the point when the constructor is invoked.
If you for instance need to examine the contents of a field of type Dictionaryduring deserialization, that dictionary may appear empty, even though it will contain entries when the entire graph has been deserialized. - IDeserializationCallback.OnDeserialization is called once the graph has been deserialized, but if multiple objects in the graph implement this interface, the ordering is not well-defined. This means that if any of your fields implement IDeserializationCallback (such as Dictionary
), you cannot rely on them being completely initialized when IDeserializationCallback.OnDeserialization is called .
(Incidentally, the workaround appears to work for Dictionary
Whether or not to use ISerializable and IDeserializationCallback is arguably a design choice, but the fairly involved mechanics makes the code harder to read for another programmer, and factoring the post-deserialization "fixup" code into a code path separate from the .NET serialization system may be a better choice when maintainability is concerned.
(See separate blog post on this.)
No comments:
Post a Comment