Exception.SerializeObjectState Exception.SerializeObjectState Exception.SerializeObjectState Exception.SerializeObjectState Event

定义

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

protected:
 event EventHandler<System::Runtime::Serialization::SafeSerializationEventArgs ^> ^ SerializeObjectState;
protected event EventHandler<System.Runtime.Serialization.SafeSerializationEventArgs> SerializeObjectState;
member this.SerializeObjectState : EventHandler<System.Runtime.Serialization.SafeSerializationEventArgs> 
Protected Custom Event SerializeObjectState As EventHandler(Of SafeSerializationEventArgs) 

示例

下面的示例定义BadDivisionException用于处理SerializeObjectState事件。The following example defines a BadDivisionException that handles the SerializeObjectState event. 它还包含一个状态对象,它是嵌套结构名为BadDivisionExceptionState实现ISafeSerializationData接口。It also contains a state object, which is a nested structure named BadDivisionExceptionState that implements the ISafeSerializationData interface.

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class Example
{
   public static void Main()
   {
      bool serialized = false;
      var formatter = new BinaryFormatter();
      Double[] values = { 3, 2, 1 };
      Double divisor = 0;
      foreach (var value in values) {
         try {
            BadDivisionException ex = null;
            if (divisor == 0) { 
               if (! serialized) {
                  // Instantiate the exception object.
                  ex = new BadDivisionException(0);
                  // Serialize the exception object.
                  var fs = new FileStream("BadDivision1.dat", 
                                           FileMode.Create);
                  formatter.Serialize(fs, ex);
                  fs.Close();
                  Console.WriteLine("Serialized the exception...");
               }
               else {
                  // Deserialize the exception.
                  var fs = new FileStream("BadDivision1.dat",
                                           FileMode.Open);
                  ex = (BadDivisionException) formatter.Deserialize(fs);
                  // Reserialize the exception.
                  fs.Position = 0;
                  formatter.Serialize(fs, ex);
                  fs.Close();
                  Console.WriteLine("Reserialized the exception...");                                            
               }   
              throw ex; 
            } 
            Console.WriteLine("{0} / {1} = {1}", value, divisor, value/divisor);
         }   
         catch (BadDivisionException e) {
            Console.WriteLine("Bad divisor from a {0} exception: {1}",
                              serialized ? "deserialized" : "new", e.Divisor);             
            serialized = true;
         }   
      }
   }
}

[Serializable] public class BadDivisionException : Exception
{
   // Maintain an internal BadDivisionException state object.
   [NonSerialized] private BadDivisionExceptionState state = new BadDivisionExceptionState();

   public BadDivisionException(Double divisor)
   {
      state.Divisor = divisor;
      HandleSerialization();      
   }
   
   private void HandleSerialization()
   {
      SerializeObjectState += delegate(object exception, SafeSerializationEventArgs eventArgs) 
                                      { 
                                          eventArgs.AddSerializedState(state);
                                      };
   }
   
   public Double Divisor
   { get { return state.Divisor; } }

   [Serializable] private struct BadDivisionExceptionState : ISafeSerializationData 
   {
      private Double badDivisor;
      
      public Double Divisor
      { get { return badDivisor; } 
        set { badDivisor = value; } }

      void ISafeSerializationData.CompleteDeserialization(object deserialized)
      { 
         var ex = deserialized as BadDivisionException;
         ex.HandleSerialization();
         ex.state = this; 
      }
   }
}
// The example displays the following output:
//       Serialized the exception...
//       Bad divisor from a new exception: 0
//       Reserialized the exception...
//       Bad divisor from a deserialized exception: 0
//       Reserialized the exception...
//       Bad divisor from a deserialized exception: 0
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
           
Module Example
   Public Sub Main()
      Dim serialized As Boolean = False
      Dim formatter As New BinaryFormatter()
      Dim values() As Double = { 3, 2, 1 }
      Dim divisor As Double = 0
      For Each value In values
         Try
            Dim ex As BadDivisionException = Nothing
            If divisor = 0 Then 
               If Not serialized Then
                  ' Instantiate the exception object.
                  ex = New BadDivisionException(0)
                  ' Serialize the exception object.
                  Dim fs As New FileStream("BadDivision1.dat", 
                                           FileMode.Create)
                  formatter.Serialize(fs, ex)
                  fs.Close()
                  Console.WriteLine("Serialized the exception...")
               Else
                  ' Deserialize the exception.
                  Dim fs As New FileStream("BadDivision1.dat",
                                           FileMode.Open)
                  ex = CType(formatter.Deserialize(fs), BadDivisionException)
                  ' Reserialize the exception.
                  fs.Position = 0
                  formatter.Serialize(fs, ex)
                  fs.Close()
                  Console.WriteLine("Reserialized the exception...")                                            
               End If   
              Throw ex 
            End If 
            Console.WriteLine("{0} / {1} = {1}", value, divisor, value/divisor)
         Catch e As BadDivisionException
            Console.WriteLine("Bad divisor from a {0} exception: {1}",
                              If(serialized, "deserialized", "new"), e.Divisor)             
            serialized = True
         End Try   
      Next
   End Sub
End Module

<Serializable> Public Class BadDivisionException : Inherits Exception
   ' Maintain an internal BadDivisionException state object.
   <NonSerialized> Private state As New BadDivisionExceptionState()

   Public Sub New(divisor As Double)
      state.Divisor = divisor
      HandleSerialization()      
   End Sub
   
   Private Sub HandleSerialization()
      AddHandler SerializeObjectState, 
                 Sub(exception As Object, eventArgs As SafeSerializationEventArgs)
                    eventArgs.AddSerializedState(state)
                 End Sub
   End Sub
   
   Public ReadOnly Property Divisor As Double
      Get
         Return state.Divisor
      End Get      
   End Property

   <Serializable> Private Structure BadDivisionExceptionState 
                                    Implements ISafeSerializationData
      private badDivisor As Double
      
      Public Property Divisor As Double
         Get
            Return badDivisor
         End Get
         Set
            badDivisor = value
         End Set
      End Property 

      Sub CompleteDeserialization(deserialized As Object) _
            Implements ISafeSerializationData.CompleteDeserialization
         Dim ex As BadDivisionException = TryCast(deserialized, BadDivisionException)
         ex.HandleSerialization()
         ex.state = Me 
      End Sub
   End Structure
End Class
' The example displays the following output:
'       Serialized the exception...
'       Bad divisor from a new exception: 0
'       Reserialized the exception...
'       Bad divisor from a deserialized exception: 0
'       Reserialized the exception...
'       Bad divisor from a deserialized exception: 0

BadDivisionException浮点除数为零发生时引发异常。The BadDivisionException exception is thrown when a floating-point division by zero occurs. 在第一个除数为零,该示例实例化BadDivisionException对象,将其,序列化,则引发异常。During the first division by zero, the example instantiates a BadDivisionException object, serializes it, and throws the exception. 出现被零后续部门,示例反序列化之前序列化的对象、 重新序列化该它,并引发异常。When subsequent divisions by zero occur, the example deserializes the previously serialized object, reserializes it, and throws the exception. 若要提供的对象序列化、 反序列化、 重新,序列化和反序列化,该示例将添加SerializeObjectState中的事件处理程序这两个BadDivisionException类构造函数并在ISafeSerializationData.CompleteDeserialization实现。To provide for object serialization, deserialization, reserialization, and deserialization, the example adds the SerializeObjectState event handler both in the BadDivisionException class constructor and in the ISafeSerializationData.CompleteDeserialization implementation.

注解

异常状态对象实现ISafeSerializationData接口。The exception state object implements the ISafeSerializationData interface.

SerializeObjectState订阅事件时,此异常将反序列化并创建为空的异常。When the 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. CompleteDeserialization异常状态对象的回调方法然后通知,以便它可以将反序列化的数据推送到空的异常。The CompleteDeserialization callback method of the exception state object is then notified so that it can push deserialized data into the empty exception.

SerializeObjectState事件使透明异常类型进行序列化和反序列化异常数据。The 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.

如果SerializeObjectState事件未订阅,像往常一样使用反序列化时发生Exception构造函数。If the SerializeObjectState event is not subscribed to, deserialization occurs as usual using the Exception constructor.

通常情况下,处理程序SerializeObjectState异常的构造函数提供的序列化过程中添加事件。Typically, a handler for the SerializeObjectState event is added in the exception's constructor to provide for its serialization. 但是,由于构造函数不是执行时SerializeObjectState事件处理程序执行时,序列化反序列化的异常可能会引发SerializationException时尝试反序列化异常的异常。But because the constructor is not executed when the SerializeObjectState event handler executes, serializing a deserialized exception can throw a SerializationException exception when you try to deserialize the exception. 若要避免此问题,您应添加的处理程序SerializeObjectState中的事件ISafeSerializationData.CompleteDeserialization方法。To avoid this, you should also add the handler for the SerializeObjectState event in the ISafeSerializationData.CompleteDeserialization method. 请参阅示例部分的说明。See the Examples section for an illustration.

继承者说明

如果此事件是订阅和使用,请遵循中的继承层次结构的所有派生的类型必须实现相同的序列化机制。If this event is subscribed to and used, all derived types that follow in the inheritance hierarchy must implement the same serialization mechanism.

适用于