次の方法で共有


TypeLoadException.GetObjectData メソッド

SerializationInfo オブジェクトに、クラス名、メソッド名、リソース ID、および追加例外情報を設定します。

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

パラメータ

  • info
    シリアル化されたオブジェクト データを保持するオブジェクト。
  • context
    転送元または転送先に関するコンテキスト情報。

実装

ISerializable.GetObjectData

例外

例外の種類 条件
ArgumentNullException info オブジェクトが null 参照 (Visual Basic では Nothing) です。

解説

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

詳細については、「 XML シリアル化および SOAP シリアル化 」を参照してください。

使用例

例外を生成し、例外データをファイルにシリアル化してから、その例外を再構築する例を次に示します。このコード例を実行するには、アセンブリの完全限定名を指定する必要があります。アセンブリの完全限定名を取得する方法については、「 アセンブリ名 」を参照してください。

 
Imports System
Imports System.Reflection
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.IO

Class GetObjectDataDemo
   Inherits Attribute

   Public Shared Sub Main()
      ' Load the mscorlib assembly and get a reference to it.
      ' You must supply the fully qualified assembly name for mscorlib.dll here.
      Dim myAssembly As [Assembly] = [Assembly].Load("Assembly text name, Version, Culture, PublicKeyToken")
      Try
         Console.WriteLine("Attempting to load a type not present in the assembly 'mscorlib'")
         ' This loading of invalid type raises a TypeLoadException
         Dim myType As Type = myAssembly.GetType("System.NonExistentType", True)
      Catch
      ' Serialize the exception to disk and reconstitute it back again.
      ' Create MyTypeLoadException instance with current time.
      ' Serialize the MyTypeLoadException instance to a file.
      ' Deserialize and reconstitute the instance from file.
         Try 
            Dim ErrorDatetime as System.DateTime = DateTime.Now
            Console.WriteLine("A TypeLoadException has been raised.")
            'Create MyTypeLoadException instance with current time.
            Dim myTypeLoadExceptionChild as MyTypeLoadExceptionChild = new MyTypeLoadExceptionChild(ErrorDatetime)
            Dim myFormatter as IFormatter  = new SoapFormatter()
            Dim myFileStream as Stream 
            myFileStream = new FileStream("typeload.xml", FileMode.Create, FileAccess.Write, FileShare.None)
            Console.WriteLine("Serializing the TypeLoadException with DateTime as " + ErrorDatetime.ToString())

            'Serialize the MyTypeLoadException instance to a file.
            myFormatter.Serialize(myFileStream, myTypeLoadExceptionChild)
            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.
            myTypeLoadExceptionChild = CType(myFormatter.Deserialize(myFileStream), MyTypeLoadExceptionChild)
            myFileStream.Close()
            Console.WriteLine("Deserialized exception has ErrorDateTime = " + myTypeLoadExceptionChild.ErrorDateTime.ToString())
         Catch e as Exception
            Console.WriteLine ("Exception :" + e.Message)
         End Try

      Catch e As Exception
         Console.WriteLine(("Exception : " + e.Message))
      End Try
   End Sub 'Main
End Class 'GetObjectDataDemo

' This class overrides the GetObjectData method and initializes
' its data with current time. 
<Serializable()> Public Class MyTypeLoadExceptionChild
   Inherits TypeLoadException
   Public ErrorDateTime As System.DateTime = DateTime.Now

   Public Sub New(myDateTime As DateTime)
      ErrorDateTime = myDateTime
   End Sub 'New

   Protected Sub New(sInfo As SerializationInfo, sContext As StreamingContext)
      ' Reconstitute the deserialized information into the instance.
      ErrorDateTime = sInfo.GetDateTime("ErrorDate")
   End Sub 'New
   
   Public Overrides Sub GetObjectData(sInfo As SerializationInfo, sContext As StreamingContext)
      ' Add a value to the Serialization information.
      sInfo.AddValue("ErrorDate", ErrorDateTime)
   End Sub 'GetObjectData
End Class 'MyTypeLoadExceptionChild

[C#] 

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

class GetObjectDataDemo : Attribute
{
   public static void Main()
   {
      // Load the mscorlib assembly and get a reference to it.
      // You must supply the fully qualified assembly name for mscorlib.dll here.
      Assembly myAssembly = Assembly.Load("Assembly text name, Version, Culture, PublicKeyToken");
      try
      {
         Console.WriteLine ("Attempting to load a type not present in the assembly 'mscorlib'");
         // This loading of invalid type raises a TypeLoadException
         Type myType = myAssembly.GetType("System.NonExistentType", true);
      }         
      catch (TypeLoadException)
      {
         // Serialize the exception to disk and reconstitute it back again.
         try 
         {
            System.DateTime ErrorDatetime = DateTime.Now;
            Console.WriteLine("A TypeLoadException has been raised.");

            // Create MyTypeLoadException instance with current time.
            MyTypeLoadExceptionChild myTypeLoadExceptionChild = new MyTypeLoadExceptionChild(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, myTypeLoadExceptionChild);
            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.
            myTypeLoadExceptionChild = (MyTypeLoadExceptionChild) myFormatter.Deserialize(myFileStream);
            myFileStream.Close();
            Console.WriteLine("Deserialized exception has ErrorDateTime = " + myTypeLoadExceptionChild.ErrorDateTime);
         }
         catch (Exception e)
         {
            Console.WriteLine ("Exception :" + e.Message);
         }
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception : " + e.Message);
      }
   }
}

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

[Serializable]
public class MyTypeLoadExceptionChild : TypeLoadException 
{
   public System.DateTime ErrorDateTime = DateTime.Now;
   public MyTypeLoadExceptionChild(DateTime myDateTime) 
   {
      ErrorDateTime = myDateTime;
   }

   protected MyTypeLoadExceptionChild(SerializationInfo sInfo, StreamingContext sContext)
   {
      // Reconstitute the deserialized information into the instance.
      ErrorDateTime=sInfo.GetDateTime("ErrorDate");
   }

   public override void GetObjectData(SerializationInfo sInfo, StreamingContext sContext) 
   {
      // Add a value to the Serialization information.
      sInfo.AddValue("ErrorDate",ErrorDateTime);
   }
}

[C++] 

#using <mscorlib.dll>
#using <System.Runtime.Serialization.Formatters.Soap.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Soap; 
using namespace System::IO;

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

[Serializable]
public __gc class MyTypeLoadExceptionChild : public TypeLoadException {
public:
   System::DateTime ErrorDateTime;
   MyTypeLoadExceptionChild() {
      ErrorDateTime = DateTime::Now;
   }
   MyTypeLoadExceptionChild(DateTime myDateTime) {
      ErrorDateTime = myDateTime;
   }
protected:
   MyTypeLoadExceptionChild(SerializationInfo* sInfo, StreamingContext* sContext) {
      // Reconstitute the deserialized information into the instance.
      ErrorDateTime=sInfo->GetDateTime(S"ErrorDate");
   }
public:
   void GetObjectData(SerializationInfo* sInfo, StreamingContext* sContext) {
      // Add a value to the Serialization information.
      sInfo->AddValue(S"ErrorDate", ErrorDateTime);
   }
};

int main() {
   // Load the mscorlib assembly and get a reference to it.
   // You must supply the fully qualified assembly name for mscorlib.dll here.
   Assembly*  myAssembly = Assembly::Load(S"Assembly text name, Version, Culture, PublicKeyToken");
   try {
      Console::WriteLine (S"Attempting to load a type not present in the assembly 'mscorlib'");
      // This loading of invalid type raises a TypeLoadException
      Type*  myType = myAssembly->GetType(S"System::NonExistentType", true);
   } catch (TypeLoadException*) {
      // Serialize the exception to disk and reconstitute it back again.
      try {
         System::DateTime ErrorDatetime = DateTime::Now;
         Console::WriteLine(S"A TypeLoadException has been raised.");

         // Create MyTypeLoadException instance with current time.
         MyTypeLoadExceptionChild* myTypeLoadExceptionChild = new MyTypeLoadExceptionChild(ErrorDatetime);
         IFormatter* myFormatter = new SoapFormatter();
         Stream* myFileStream = new FileStream(S"typeload.xml", FileMode::Create, FileAccess::Write, FileShare::None);
         Console::WriteLine(S"Serializing the TypeLoadException with DateTime as {0}", __box(ErrorDatetime));

         // Serialize the MyTypeLoadException instance to a file.
         myFormatter->Serialize(myFileStream, myTypeLoadExceptionChild);
         myFileStream->Close();
         Console::WriteLine(S"Deserializing the Exception.");
         myFileStream = new FileStream(S"typeload.xml", FileMode::Open, FileAccess::Read, FileShare::None);

         // Deserialize and reconstitute the instance from file.
         myTypeLoadExceptionChild = __try_cast<MyTypeLoadExceptionChild*>(myFormatter->Deserialize(myFileStream));
         myFileStream->Close();
         Console::WriteLine(S"Deserialized exception has ErrorDateTime = {0}", __box(myTypeLoadExceptionChild->ErrorDateTime));
      } catch (Exception* e) {
         Console::WriteLine (S"Exception : {0}", e->Message);
      }
   } catch (Exception* e) {
      Console::WriteLine(S"Exception : {0}", e->Message);
   }
}

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

必要条件

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

参照

TypeLoadException クラス | TypeLoadException メンバ | System 名前空間 | SerializationInfo | XML シリアル化および SOAP シリアル化