Exception.GetObjectData(SerializationInfo, StreamingContext) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
주의
This API supports obsolete formatter-based serialization. It should not be called or extended by application code.
파생 클래스에서 재정의된 경우 예외에 관한 정보를 SerializationInfo 에 설정합니다.
public:
virtual void GetObjectData(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
public virtual void GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public virtual void GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[System.Security.SecurityCritical]
public virtual void GetObjectData (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
abstract member GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
override this.GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
abstract member GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
override this.GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
[<System.Security.SecurityCritical>]
abstract member GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
override this.GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
Public Overridable Sub GetObjectData (info As SerializationInfo, context As StreamingContext)
매개 변수
- info
- SerializationInfo
throw되는 예외에 대해 serialize된 개체 데이터를 보유하는 SerializationInfo입니다.
- context
- StreamingContext
소스 또는 대상에 대한 컨텍스트 정보를 포함하는 StreamingContext입니다.
구현
- 특성
예외
info
매개 변수가 null 참조(Visual Basic에서는 Nothing
)인 경우
예제
다음 코드 예제에서는 를 구현GetObjectData
하는 파생 직렬화 가능 Exception
클래스를 정의합니다. 이 클래스는 두 속성을 약간 변경한 다음 기본 클래스를 호출하여 serialization을 수행합니다. 이 예제에서는 0으로 나누기 오류를 강제로 수행한 다음 파생된 예외의 instance 만듭니다. 코드는 instance 파일에 직렬화하고 파일을 throw하는 새 예외로 역직렬화한 다음 예외의 데이터를 catch하고 표시합니다.
#using <System.Runtime.Serialization.Formatters.Soap.dll>
using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Soap;
// Define a serializable derived exception class.
[Serializable]
ref class SecondLevelException: public Exception, public ISerializable
{
public:
// This public constructor is used by class instantiators.
SecondLevelException( String^ message, Exception^ inner )
: Exception( message, inner )
{
HelpLink = "http://MSDN.Microsoft.com";
Source = "Exception_Class_Samples";
}
protected:
// This protected constructor is used for deserialization.
SecondLevelException( SerializationInfo^ info, StreamingContext context )
: Exception( info, context )
{}
public:
// GetObjectData performs a custom serialization.
[System::Security::Permissions::SecurityPermissionAttribute
(System::Security::Permissions::SecurityAction::LinkDemand,
Flags=System::Security::Permissions::SecurityPermissionFlag::SerializationFormatter)]
virtual void GetObjectData( SerializationInfo^ info, StreamingContext context ) override
{
// Change the case of two properties, and then use the
// method of the base class.
HelpLink = HelpLink->ToLower();
Source = Source->ToUpperInvariant();
Exception::GetObjectData( info, context );
}
};
int main()
{
Console::WriteLine( "This example of the Exception constructor "
"and Exception.GetObjectData\nwith Serialization"
"Info and StreamingContext parameters "
"generates \nthe following output.\n" );
try
{
// This code forces a division by 0 and catches the
// resulting exception.
try
{
int zero = 0;
int ecks = 1 / zero;
}
catch ( Exception^ ex )
{
// Create a new exception to throw again.
SecondLevelException^ newExcept = gcnew SecondLevelException( "Forced a division by 0 and threw "
"another exception.",ex );
Console::WriteLine( "Forced a division by 0, caught the "
"resulting exception, \n"
"and created a derived exception:\n" );
Console::WriteLine( "HelpLink: {0}", newExcept->HelpLink );
Console::WriteLine( "Source: {0}", newExcept->Source );
// This FileStream is used for the serialization.
FileStream^ stream = gcnew FileStream( "NewException.dat",FileMode::Create );
try
{
// Serialize the derived exception.
SoapFormatter^ formatter = gcnew SoapFormatter( nullptr,StreamingContext(StreamingContextStates::File) );
formatter->Serialize( stream, newExcept );
// Rewind the stream and deserialize the
// exception.
stream->Position = 0;
SecondLevelException^ deserExcept = dynamic_cast<SecondLevelException^>(formatter->Deserialize( stream ));
Console::WriteLine( "\nSerialized the exception, and then "
"deserialized the resulting stream "
"into a \nnew exception. "
"The deserialization changed the case "
"of certain properties:\n" );
// Throw the deserialized exception again.
throw deserExcept;
}
catch ( SerializationException^ se )
{
Console::WriteLine( "Failed to serialize: {0}", se->ToString() );
}
finally
{
stream->Close();
}
}
}
catch ( Exception^ ex )
{
Console::WriteLine( "HelpLink: {0}", ex->HelpLink );
Console::WriteLine( "Source: {0}", ex->Source );
Console::WriteLine();
Console::WriteLine( ex->ToString() );
}
}
/*
This example of the Exception constructor and Exception.GetObjectData
with SerializationInfo and StreamingContext parameters generates
the following output.
Forced a division by 0, caught the resulting exception,
and created a derived exception:
HelpLink: http://MSDN.Microsoft.com
Source: Exception_Class_Samples
Serialized the exception, and then deserialized the resulting stream into a
new exception. The deserialization changed the case of certain properties:
HelpLink: http://msdn.microsoft.com
Source: EXCEPTION_CLASS_SAMPLES
SecondLevelException: Forced a division by 0 and threw another exception. ---> S
ystem.DivideByZeroException: Attempted to divide by zero.
at main()
--- End of inner exception stack trace ---
at main()
*/
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
// Define a serializable derived exception class.
[Serializable]
class SecondLevelException : Exception, ISerializable
{
// This public constructor is used by class instantiators.
public SecondLevelException(string message, Exception inner) :
base(message, inner)
{
HelpLink = "http://MSDN.Microsoft.com";
Source = "Exception_Class_Samples";
}
// This protected constructor is used for deserialization.
protected SecondLevelException(SerializationInfo info,
StreamingContext context) :
base(info, context)
{ }
// GetObjectData performs a custom serialization.
public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
// Change the case of two properties, and then use the
// method of the base class.
HelpLink = HelpLink.ToLower();
Source = Source.ToUpperInvariant();
base.GetObjectData(info, context);
}
}
class SerializationDemo
{
public static void Main()
{
Console.WriteLine(
"This example of the Exception constructor " +
"and Exception.GetObjectData\nwith Serialization" +
"Info and StreamingContext parameters " +
"generates \nthe following output.\n");
try
{
// This code forces a division by 0 and catches the
// resulting exception.
try
{
int zero = 0;
int ecks = 1 / zero;
}
catch (Exception ex)
{
// Create a new exception to throw again.
SecondLevelException newExcept =
new SecondLevelException(
"Forced a division by 0 and threw " +
"another exception.", ex);
Console.WriteLine(
"Forced a division by 0, caught the " +
"resulting exception, \n" +
"and created a derived exception:\n");
Console.WriteLine("HelpLink: {0}",
newExcept.HelpLink);
Console.WriteLine("Source: {0}",
newExcept.Source);
// This FileStream is used for the serialization.
FileStream stream =
new FileStream("NewException.dat",
FileMode.Create);
try
{
// Serialize the derived exception.
SoapFormatter formatter =
new SoapFormatter(null,
new StreamingContext(
StreamingContextStates.File));
formatter.Serialize(stream, newExcept);
// Rewind the stream and deserialize the
// exception.
stream.Position = 0;
SecondLevelException deserExcept =
(SecondLevelException)
formatter.Deserialize(stream);
Console.WriteLine(
"\nSerialized the exception, and then " +
"deserialized the resulting stream " +
"into a \nnew exception. " +
"The deserialization changed the case " +
"of certain properties:\n");
// Throw the deserialized exception again.
throw deserExcept;
}
catch (SerializationException se)
{
Console.WriteLine("Failed to serialize: {0}",
se.ToString());
}
finally
{
stream.Close();
}
}
}
catch (Exception ex)
{
Console.WriteLine("HelpLink: {0}", ex.HelpLink);
Console.WriteLine("Source: {0}", ex.Source);
Console.WriteLine();
Console.WriteLine(ex.ToString());
}
}
}
/*
This example displays the following output.
Forced a division by 0, caught the resulting exception,
and created a derived exception:
HelpLink: http://MSDN.Microsoft.com
Source: Exception_Class_Samples
Serialized the exception, and then deserialized the resulting stream into a
new exception. The deserialization changed the case of certain properties:
HelpLink: http://msdn.microsoft.com
Source: EXCEPTION_CLASS_SAMPLES
NDP_UE_CS.SecondLevelException: Forced a division by 0 and threw another except
ion. ---> System.DivideByZeroException: Attempted to divide by zero.
at NDP_UE_CS.SerializationDemo.Main()
--- End of inner exception stack trace ---
at NDP_UE_CS.SerializationDemo.Main()
*/
open System
open System.IO
open System.Runtime.Serialization
open System.Runtime.Serialization.Formatters.Soap
open System.Security.Permissions
// Define a serializable derived exception class.
[<Serializable>]
type SecondLevelException =
inherit Exception
interface ISerializable with
// GetObjectData performs a custom serialization.
member this.GetObjectData(info: SerializationInfo, context: StreamingContext) =
// Change the case of two properties, and then use the
// method of the base class.
this.HelpLink <- this.HelpLink.ToLower()
this.Source <- this.Source.ToUpperInvariant()
base.GetObjectData( info, context )
// This public constructor is used by class instantiators.
new (message: string, inner: Exception) as this =
{ inherit Exception(message, inner) }
then
this.HelpLink <- "http://MSDN.Microsoft.com"
this.Source <- "Exception_Class_Samples"
// This protected constructor is used for deserialization.
new (info: SerializationInfo, context: StreamingContext) =
{ inherit Exception(info, context) }
printfn
"""This example of the Exception constructor and Exception.GetObjectData
with SerializationInfo and StreamingContext parameters generates
the following output.
"""
try
// This code forces a division by 0 and catches the
// resulting exception.
try
let zero = 0
let ecks = 1 / zero
()
with ex ->
// Create a new exception to throw again.
let newExcept = SecondLevelException("Forced a division by 0 and threw another exception.", ex)
printfn "Forced a division by 0, caught the resulting exception, \nand created a derived exception:\n"
printfn $"HelpLink: {newExcept.HelpLink}"
printfn $"Source: {newExcept.Source}"
// This FileStream is used for the serialization.
use stream = new FileStream("NewException.dat", FileMode.Create)
try
// Serialize the derived exception.
let formatter = SoapFormatter(null, StreamingContext StreamingContextStates.File)
formatter.Serialize(stream, newExcept)
// Rewind the stream and deserialize the
// exception.
stream.Position <- 0L
let deserExcept = formatter.Deserialize stream :?> SecondLevelException
printfn
"""
Serialized the exception, and then deserialized the resulting stream into a
new exception. The deserialization changed the case of certain properties:
"""
// Throw the deserialized exception again.
raise deserExcept
with :? SerializationException as se ->
printfn $"Failed to serialize: {se}"
with ex ->
printfn $"HelpLink: {ex.HelpLink}"
printfn $"Source: {ex.Source}"
printfn $"\n{ex}"
// This example displays the following output.
// Forced a division by 0, caught the resulting exception,
// and created a derived exception:
//
// HelpLink: http://MSDN.Microsoft.com
// Source: Exception_Class_Samples
//
// Serialized the exception, and then deserialized the resulting stream into a
// new exception. The deserialization changed the case of certain properties:
//
// HelpLink: http://msdn.microsoft.com
// Source: EXCEPTION_CLASS_SAMPLES
//
// NDP_UE_FS_3+SecondLevelException: Forced a division by 0 and threw another except
// ion. ---> System.DivideByZeroException: Attempted to divide by zero.
// at <StartupCode$fs>.$NDP_UE_FS_3.main@()
// --- End of inner exception stack trace ---
// at <StartupCode$fs>.$NDP_UE_FS_3.main@()
' If compiling with the Visual Basic compiler (vbc.exe) from the command
' prompt, be sure to add the following switch:
' /reference:System.Runtime.Serialization.Formatters.Soap.dll
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.Security.Permissions
' Define a serializable derived exception class.
<Serializable()> _
Class SecondLevelException
Inherits Exception
' This public constructor is used by class instantiators.
Public Sub New( message As String, inner As Exception )
MyBase.New( message, inner )
HelpLink = "http://MSDN.Microsoft.com"
Source = "Exception_Class_Samples"
End Sub
' This protected constructor is used for deserialization.
Protected Sub New( info As SerializationInfo, _
context As StreamingContext )
MyBase.New( info, context )
End Sub
' GetObjectData performs a custom serialization.
<SecurityPermissionAttribute(SecurityAction.Demand, _
SerializationFormatter:=True)> _
Overrides Sub GetObjectData( info As SerializationInfo, _
context As StreamingContext)
' Change the case of two properties, and then use the
' method of the base class.
HelpLink = HelpLink.ToLower()
Source = Source.ToUpperInvariant()
MyBase.GetObjectData(info, context)
End Sub
End Class
Module SerializationDemo
Sub Main()
Console.WriteLine( _
"This example of the Exception constructor " & _
"and Exception.GetObjectData " & vbCrLf & _
"with SerializationInfo and StreamingContext " & _
"parameters generates " & vbCrLf & _
"the following output." & vbCrLf )
' This code forces a division by 0 and catches the
' resulting exception.
Try
Try
Dim zero As Integer = 0
Dim ecks As Integer = 1 \ zero
' Create a new exception to throw again.
Catch ex As Exception
Dim newExcept As New SecondLevelException( _
"Forced a division by 0 and threw " & _
"another exception.", ex )
Console.WriteLine( _
"Forced a division by 0, caught the " & _
"resulting exception, " & vbCrLf & _
"and created a derived exception:" & vbCrLf )
Console.WriteLine( "HelpLink: {0}", _
newExcept.HelpLink )
Console.WriteLine( "Source: {0}", _
newExcept.Source )
' This FileStream is used for the serialization.
Dim stream As New FileStream( _
"NewException.dat", FileMode.Create )
' Serialize the derived exception.
Try
Dim formatter As New SoapFormatter( Nothing, _
New StreamingContext( _
StreamingContextStates.File ) )
formatter.Serialize( stream, newExcept )
' Rewind the stream and deserialize the
' exception.
stream.Position = 0
Dim deserExcept As SecondLevelException = _
CType( formatter.Deserialize( stream ), _
SecondLevelException )
Console.WriteLine( vbCrLf & _
"Serialized the exception, and then " & _
"deserialized the resulting stream " & _
"into a " & vbCrLf & "new exception. " & _
"The deserialization changed the case " & _
"of certain properties:" & vbCrLf )
' Throw the deserialized exception again.
Throw deserExcept
Catch se As SerializationException
Console.WriteLine( "Failed to serialize: {0}", _
se.ToString( ) )
Finally
stream.Close( )
End Try
End Try
Catch ex As Exception
Console.WriteLine( "HelpLink: {0}", ex.HelpLink )
Console.WriteLine( "Source: {0}", ex.Source )
Console.WriteLine( )
Console.WriteLine( ex.ToString( ) )
End Try
End Sub
End Module
' This example displays the following output:
'
' Forced a division by 0, caught the resulting exception,
' and created a derived exception:
'
' HelpLink: http://MSDN.Microsoft.com
' Source: Exception_Class_Samples
'
' Serialized the exception, and then deserialized the resulting stream into a
' new exception. The deserialization changed the case of certain properties:
'
' HelpLink: http://msdn.microsoft.com
' Source: EXCEPTION_CLASS_SAMPLES
'
' NDP_UE_VB.SecondLevelException: Forced a division by 0 and threw another exce
' ption. ---> System.DivideByZeroException: Attempted to divide by zero.
' at NDP_UE_VB.SerializationDemo.Main()
' --- End of inner exception stack trace ---
' at NDP_UE_VB.SerializationDemo.Main()
설명
GetObjectData
은 직렬화를 대상으로 하는 모든 예외 개체 데이터를 사용하여 SerializationInfo를 설정합니다. 역직렬화하는 동안 스트림을 통해 전송된 SerializationInfo
에서 예외가 다시 구성됩니다.
적용 대상
추가 정보
.NET