This documentation is archived and is not being maintained.

TypeLoadException.GetObjectData Method

Sets the SerializationInfo object with the class name, method name, resource ID, and additional exception information.

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

Parameters

info
The object that holds the serialized object data.
context
The contextual information about the source or destination.

Implements

ISerializable.GetObjectData

Exceptions

Exception Type Condition
ArgumentNullException The info object is a null reference (Nothing in Visual Basic).

Remarks

GetObjectData sets a SerializationInfo with all the exception object data targeted for serialization. During deserialization, the exception object is reconstituted from the SerializationInfo transmitted over the stream.

For more information, see XML and SOAP Serialization.

Example

The following example generates an exception, and serializes the exception data to a file, and then reconstitutes the exception. For this code example to run, you must provide the fully qualified assembly name. For information about how to obtain the fully qualified assembly name, see Assembly Names.

[Visual Basic] 
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] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

TypeLoadException Class | TypeLoadException Members | System Namespace | SerializationInfo | XML and SOAP Serialization

Show: