次の方法で共有


Exception.GetObjectData メソッド

派生クラスでオーバーライドされた場合は、その例外に関する情報を使用して SerializationInfo を設定します。

Public Overridable Sub GetObjectData( _
   ByVal info As SerializationInfo, _   ByVal context As StreamingContext _) Implements ISerializable.GetObjectData
[C#]
public virtual void GetObjectData(SerializationInfoinfo,StreamingContextcontext);
[C++]
public: virtual void GetObjectData(SerializationInfo* info,StreamingContextcontext);
[JScript]
public function GetObjectData(
   info : SerializationInfo,context : StreamingContext);

パラメータ

  • info
    スローされている例外に関するシリアル化済みオブジェクト データを保持している SerializationInfo
  • context
    転送元または転送先に関するコンテキスト情報を含んでいる StreamingContext

実装

ISerializable.GetObjectData

例外

例外の種類 条件
ArgumentNullException info パラメータが null 参照 (Visual Basic の場合は Nothing) です。

解説

GetObjectData は、シリアル化する対象のすべての例外オブジェクト データを使用して、 SerializationInfo を設定します。逆シリアル化中に、ストリームで転送された SerializationInfo から例外が再構成されます。

使用例

[Visual Basic, C#] GetObjectData を実装するシリアル化可能な派生 Exception クラスを定義するコード例を次に示します。 Exception クラスにより、2 つのプロパティに小さな変更が行われ、その後に基本クラスが呼び出されてシリアル化が実行されます。この例では、0 による除算エラーを強制し、次に派生 Exception クラスのインスタンスを作成します。インスタンスをファイルにシリアル化し、そのファイルを逆シリアル化して新しい Exception をスローし、その例外のデータをキャッチして表示します。

 
' Example for the Exception( SerializationInfo, StreamingContext )
' constructor and the Exception.GetObjectData( SerializationInfo, 
' StreamingContext ) method.
'
' If compiling with the Visual Basic compiler (VBC) from the command 
' prompt, be sure to add the following switch:
'    /reference:System.Runtime.Serialization.Formatters.Soap.dll 
Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap
Imports Microsoft.VisualBasic

Namespace NDP_UE_VB

    ' 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 = "https://MSDN.Microsoft.com"
            Source = "Exception_Class_Samples"
        End Sub ' New
       
        ' This protected constructor is used for deserialization.
        Protected Sub New( info As SerializationInfo, _
            context As StreamingContext )
                MyBase.New( info, context )
        End Sub ' New
           
           
        ' GetObjectData performs a custom serialization.
        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.ToUpper()
              
            MyBase.GetObjectData(info, context)

        End Sub ' ISerializable.GetObjectData
    End Class ' SecondLevelException


    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 ' Main

    End Module ' SerializationDemo
End Namespace ' NDP_UE_VB

' 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: https://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: https://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()

[C#] 
// Example for the Exception( SerializationInfo, StreamingContext )
// constructor and the Exception.GetObjectData( SerializationInfo, 
// StreamingContext ) method.
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;

namespace NDP_UE_CS
{
    // 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 = "https://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.ToUpper( );

            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 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: https://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: https://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()
*/

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Exception クラス | Exception メンバ | System 名前空間 | SerializationInfo | StreamingContext