System.TypeLoadException.GetObjectData 方法

方法描述

使用类名、方法名称、资源 ID 和附加异常信息来设置 SerializationInfo 对象。

语法定义(C# System.TypeLoadException.GetObjectData 方法 的用法)

public override void GetObjectData(
	SerializationInfo info,
	StreamingContext context
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
info System-Runtime-Serialization-SerializationInfo 保存序列化对象数据的对象。
context System-Runtime-Serialization-StreamingContext 有关源或目标的上下文信息。
返回值 void

提示和注释

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

有关更多信息,请参见 []。

System.TypeLoadException.GetObjectData 方法例子

有关如何获取完全限定程序集名称的信息,请参见 程序集名称。

using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap; 
using System.Security.Permissions;
using System.IO;

class GetObjectDataDemo
{
   public static void Main()
   {
      // Get a reference to the assembly mscorlib.dll, which is always
      // loaded. (System.String is defined in mscorlib.)
      Assembly mscorlib = typeof(string).Assembly;

      try
      {
         Console.WriteLine ("Attempting to load a type not present in the assembly 'mscorlib'");
         // This loading of invalid type raises a TypeLoadException
         Type myType = mscorlib.GetType("System.NonExistentType", true);
      }         
      catch (TypeLoadException)
      {
         // Serialize the exception to disk and reconstitute it.
         System.DateTime ErrorDatetime = DateTime.Now;
         Console.WriteLine("A TypeLoadException has been raised.");

         // Create MyTypeLoadException instance with current time.
         MyTypeLoadException myException = new MyTypeLoadException(ErrorDatetime);
         IFormatter myFormatter = new SoapFormatter();
         Stream myFileStream = new FileStream("typeload.xml", FileMode.Create, FileAccess.Write, FileShare.None);
         Console.WriteLine("Serializing the TypeLoadException with DateTime as " + ErrorDatetime);

         // Serialize the MyTypeLoadException instance to a file.
         myFormatter.Serialize(myFileStream, myException);
         myFileStream.Close();

         Console.WriteLine("Deserializing the Exception.");
         myFileStream = new FileStream("typeload.xml", FileMode.Open, FileAccess.Read, FileShare.None);

         // Deserialize and reconstitute the instance from file.
         myException = (MyTypeLoadException) myFormatter.Deserialize(myFileStream);
         myFileStream.Close();
         Console.WriteLine("Deserialized exception has ErrorDateTime = " + myException.ErrorDateTime);
      }
   }
}

// This class overrides the GetObjectData method and initializes
// its data with current time. 

[Serializable]
public class MyTypeLoadException : TypeLoadException 
{
   private System.DateTime _errorDateTime = DateTime.Now;
   public DateTime ErrorDateTime { get { return _errorDateTime; }}

   public MyTypeLoadException(DateTime myDateTime) 
   {
      _errorDateTime = myDateTime;
   }

   protected MyTypeLoadException(SerializationInfo sInfo, StreamingContext sContext) 
       : base(sInfo, sContext)
   {
      // Reconstitute the deserialized information into the instance.
      _errorDateTime = sInfo.GetDateTime("ErrorDate");
   }

   // GetObjectData overrides must always have a demand for SerializationFormatter.
   [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter=true)]
   public override void GetObjectData(SerializationInfo sInfo, StreamingContext sContext) 
   {
      base.GetObjectData(sInfo, sContext);
      // Add a value to the Serialization information.
      sInfo.AddValue("ErrorDate", ErrorDateTime);
   }
}

异常

异常 异常描述
ArgumentNullException info 对象为 null。

命名空间

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 系统要求。