當前位置: 首頁>>代碼示例>>C#>>正文


C# Exception.GetObjectData方法代碼示例

本文整理匯總了C#中System.Exception.GetObjectData方法的典型用法代碼示例。如果您正苦於以下問題:C# Exception.GetObjectData方法的具體用法?C# Exception.GetObjectData怎麽用?C# Exception.GetObjectData使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Exception的用法示例。


在下文中一共展示了Exception.GetObjectData方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: SecondLevelException

//引入命名空間
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Security.Permissions;

 // 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.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( ) );
         }
     }
 }
開發者ID:.NET開發者,項目名稱:System,代碼行數:127,代碼來源:Exception.GetObjectData

輸出:

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()


注:本文中的System.Exception.GetObjectData方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。