System.Exception.GetObjectData 方法

方法描述

当在派生类中重写时,用关于异常的信息设置 SerializationInfo。

语法定义

public virtual void GetObjectData(
	SerializationInfo info,
	StreamingContext context
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
info System-Runtime-Serialization-SerializationInfo SerializationInfo ,它存有有关所引发异常的序列化的对象数据。
context System-Runtime-Serialization-StreamingContext StreamingContext ,它包含有关源或目标的上下文信息。
返回值 void

提示和注释

GetObjectData 用针对于序列化的所有异常对象数据设置 SerializationInfo。 反序列化期间,从通过流传输的 SerializationInfo 重建异常。

System.Exception.GetObjectData 方法例子

代码将该实例序列化为一个文件,并将该文件反序列化为一个它引发的新异常,然后捕捉并显示该异常的数据。

// 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;
using System.Security.Permissions;

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 = "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.
        [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
        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: 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()
*/

异常

异常 异常描述
ArgumentNullException info 参数是空引用(Visual Basic 中为 Nothing)。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。