Exception.SerializeObjectState Ereignis
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Achtung
BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.
Tritt auf, wenn eine Ausnahme serialisiert wird, um ein Ausnahmezustandsobjekt mit serialisierten Daten über die Ausnahme zu erstellen.
protected:
event EventHandler<System::Runtime::Serialization::SafeSerializationEventArgs ^> ^ SerializeObjectState;
protected event EventHandler<System.Runtime.Serialization.SafeSerializationEventArgs>? SerializeObjectState;
[System.Obsolete("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId="SYSLIB0011", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected event EventHandler<System.Runtime.Serialization.SafeSerializationEventArgs>? SerializeObjectState;
protected event EventHandler<System.Runtime.Serialization.SafeSerializationEventArgs> SerializeObjectState;
member this.SerializeObjectState : EventHandler<System.Runtime.Serialization.SafeSerializationEventArgs>
[<System.Obsolete("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId="SYSLIB0011", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
member this.SerializeObjectState : EventHandler<System.Runtime.Serialization.SafeSerializationEventArgs>
Protected Custom Event SerializeObjectState As EventHandler(Of SafeSerializationEventArgs)
Ereignistyp
- Attribute
Beispiele
Im folgenden Beispiel wird ein BadDivisionException
Ereignis definiert, das das SerializeObjectState Ereignis behandelt. Es enthält auch ein Zustandsobjekt, das eine geschachtelte Struktur mit dem Namen BadDivisionExceptionState
ist, die die ISafeSerializationData Schnittstelle implementiert.
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
Die BadDivisionException
Ausnahme wird ausgelöst, wenn eine Gleitkommateilung nach Null auftritt. Während der ersten Division um Null instanziiert das Beispiel ein BadDivisionException
Objekt, serialisiert es und löst die Ausnahme aus. Wenn nachfolgende Divisionen nach Null auftreten, wird das Beispiel deserialisiert, das zuvor serialisierte Objekt, die Größe ändert, und löst die Ausnahme aus. Um die Objekt serialisierung, Deserialisierung, Reserialisierung und Deserialisierung bereitzustellen, fügt das Beispiel den SerializeObjectState Ereignishandler sowohl im Klassenkonstruktor als auch in der BadDivisionException
ISafeSerializationData.CompleteDeserialization Implementierung hinzu.
Hinweise
Das Ausnahmezustandsobjekt implementiert die ISafeSerializationData Schnittstelle.
Wenn das Ereignis abonniert wird, wird die SerializeObjectState Ausnahme als leere Ausnahme deserialisiert und erstellt. Der Konstruktor der Ausnahme wird nicht ausgeführt, und der Ausnahmezustand wird ebenfalls deserialisiert. Die CompleteDeserialization Rückrufmethode des Ausnahmezustandsobjekts wird dann benachrichtigt, sodass sie deerialisierte Daten in die leere Ausnahme pushen kann.
Das SerializeObjectState Ereignis ermöglicht transparente Ausnahmetypen zum Serialisieren und Deserialisieren von Ausnahmedaten. Transparenter Code kann Befehle innerhalb der Grenzen des Berechtigungssatzes ausführen, in dem er ausgeführt wird, aber nicht ausgeführt, aufgerufen, von oder enthält kritischen Code.
Wenn das Ereignis nicht abonniert ist, tritt die SerializeObjectState Deserialisierung wie üblich mit dem Exception Konstruktor auf.
Normalerweise wird ein Handler für das Ereignis im Konstruktor der SerializeObjectState Ausnahme hinzugefügt, um seine Serialisierung bereitzustellen. Da der Konstruktor jedoch nicht ausgeführt wird, wenn der SerializeObjectState Ereignishandler ausgeführt wird, kann die Serialisierung einer deserialisierten Ausnahme eine SerializationException Ausnahme auslösen, wenn Sie versuchen, die Ausnahme zu enterialisieren. Um dies zu vermeiden, sollten Sie auch den Handler für das SerializeObjectState Ereignis in der ISafeSerializationData.CompleteDeserialization Methode hinzufügen. Weitere Informationen finden Sie im Abschnitt "Beispiele" für eine Abbildung.
Hinweise für Vererber
Wenn dieses Ereignis abonniert und verwendet wird, müssen alle abgeleiteten Typen, die in der Vererbungshierarchie folgen, den gleichen Serialisierungsmechanismus implementieren.