TypeLoadException::GetObjectData Method (SerializationInfo^, StreamingContext)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

virtual void GetObjectData(
	SerializationInfo^ info,
	StreamingContext context
) override


Type: System.Runtime.Serialization::SerializationInfo^

The object that holds the serialized object data.

Type: System.Runtime.Serialization::StreamingContext

The contextual information about the source or destination.

Exception Condition

The info object is null.

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.

[Visual Basic, C#]

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.

#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. 

public ref class MyTypeLoadExceptionChild: public TypeLoadException
   System::DateTime ErrorDateTime;
      ErrorDateTime = DateTime::Now;

   MyTypeLoadExceptionChild( DateTime myDateTime )
      ErrorDateTime = myDateTime;

   MyTypeLoadExceptionChild( SerializationInfo^ sInfo, StreamingContext * sContext )

      // Reconstitute the deserialized information into the instance.
      ErrorDateTime = sInfo->GetDateTime( "ErrorDate" );

   void GetObjectData( SerializationInfo^ sInfo, StreamingContext * sContext )

      // Add a value to the Serialization information.
      sInfo->AddValue( "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( "Assembly text name, Version, Culture, PublicKeyToken" );
      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.
         System::DateTime ErrorDatetime = DateTime::Now;
         Console::WriteLine( "A TypeLoadException has been raised." );

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

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

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

   catch ( Exception^ e ) 
      Console::WriteLine( "Exception : {0}", e->Message );



Requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.

.NET Framework
Available since 1.1
Return to top