Use the OnSerializingAttribute class to manipulate the object before serialization occurs.

To use the OnSerializingAttributeclass, the method must contain a StreamingContext parameter. The attribute marks the method to be called by the serialization infrastructure, and the StreamingContext provides additional data about the type of serialization taking place. The following code shows an example of how to apply OnSerializingAttribute to a method.