O'Reilly logo

C# in a Nutshell by Peter Drayton, Ted Neward, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

IDeserializationCallback

It is helpful to think of deserialization as a special form of constructor call, since the end result is a valid object reference. While the [NonSerialized] attribute is useful for controlling which data is stored in the serialized form of the object or sent over the wire on a remoting call, the problem is that the fields marked with [NonSerialized] are not assigned meaningful values when the constructor (i.e. the deserialization process) finishes executing. This can result in class invariants being violated, such as the implicit in the preceding sample, in which Age is intended to store the Person’s age based on the DateOfBirth.

One possible solution to this problem is the IDeserializationCallback interface. Implementing this interface on your type indicates to the runtime that you wish to participate in the deserialization process. This interface contains a single method, OnDeserialization, which the runtime invokes after it has finished constructing your object, but before it returns the fully constructed instance to the client. This method gives you a chance to perform whatever fixups you need to inside the object, and is a way to ensure that class invariants are preserved before clients can access the instance.

Using our sample Person class, the IDeserializationCallback interface implementation might look like this:

[Serializable] public sealed class Person : IDeserializationCallback { public string Name; public DateTime DateOfBirth; [NonSerialized] public ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required