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.

 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", 
                  formatter.Serialize(fs, ex);
                  Console.WriteLine("Serialized the exception...");
               else {
                  // Deserialize the exception.
                  var fs = new FileStream("BadDivision1.dat",
                  ex = (BadDivisionException) formatter.Deserialize(fs);
                  // Reserialize the exception.
                  fs.Position = 0;
                  formatter.Serialize(fs, ex);
                  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;
   private void HandleSerialization()
      SerializeObjectState += delegate(object exception, SafeSerializationEventArgs eventArgs) 
   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.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
            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", 
                  formatter.Serialize(fs, ex)
                  Console.WriteLine("Serialized the exception...")
                  ' Deserialize the exception.
                  Dim fs As New FileStream("BadDivision1.dat",
                  ex = CType(formatter.Deserialize(fs), BadDivisionException)
                  ' Reserialize the exception.
                  fs.Position = 0
                  formatter.Serialize(fs, ex)
                  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   
   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
   End Sub
   Private Sub HandleSerialization()
      AddHandler SerializeObjectState, 
                 Sub(exception As Object, eventArgs As SafeSerializationEventArgs)
                 End Sub
   End Sub
   Public ReadOnly Property Divisor As Double
         Return state.Divisor
      End Get      
   End Property

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

      Sub CompleteDeserialization(deserialized As Object) _
            Implements ISafeSerializationData.CompleteDeserialization
         Dim ex As BadDivisionException = TryCast(deserialized, BadDivisionException)
         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 0 による浮動小数点除算が発生したときに例外がスローされます。The BadDivisionException exception is thrown when a floating-point division by zero occurs. 例では、インスタンス化中 0 による除算の最初に、BadDivisionExceptionオブジェクト、シリアル化、および例外をスローします。During the first division by zero, the example instantiates a BadDivisionException object, serializes it, and throws the exception. 0 による後続の部門が発生すると、例が既にシリアル化されたオブジェクトを逆シリアル化、なって、および例外をスローします。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.