System.Exception.SerializeObjectState Event

Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

Syntax

protected event EventHandler<System.Runtime.Serialization.SafeSerializationEventArgs> SerializeObjectState

Remarks

The exception state object implements the System.Runtime.Serialization.ISafeSerializationData interface.

When the Exception.SerializeObjectState event is subscribed to, the exception is deserialized and created as an empty exception. The exception's constructor is not run, and the exception state is also deserialized. The System.Runtime.Serialization.ISafeSerializationData.CompleteDeserialization(object) callback method of the exception state object is then notified so that it can push deserialized data into the empty exception.

The Exception.SerializeObjectState event enables transparent exception types to serialize and deserialize exception data. Transparent code can execute commands within the bounds of the permission set it is operating within, but cannot execute, call, derive from, or contain critical code.

If the Exception.SerializeObjectState event is not subscribed to, deserialization occurs as usual using the Exception.#ctor(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext) constructor.

Typically, a handler for the Exception.SerializeObjectState event is added in the exception's constructor to provide for its serialization. But because the constructor is not executed when the Exception.SerializeObjectState event handler executes, serializing a deserialized exception can throw a System.Runtime.Serialization.SerializationException exception when you try to deserialize the exception. To avoid this, you should also add the handler for the Exception.SerializeObjectState event in the System.Runtime.Serialization.ISafeSerializationData.CompleteDeserialization(object) method. See the Examples section for an illustration.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 4.0.0.0