TypeLoadException::GetObjectData Method
Sets the SerializationInfo object with the class name, method name, resource ID, and additional exception information.
Assembly: mscorlib (in mscorlib.dll)
public: virtual void GetObjectData( SerializationInfo^ info, StreamingContext context ) override
Parameters
- info
- Type: System.Runtime.Serialization::SerializationInfo
The object that holds the serialized object data.
- context
- Type: System.Runtime.Serialization::StreamingContext
The contextual information about the source or destination.
Implements
ISerializable::GetObjectData(SerializationInfo, StreamingContext)ISerializable::GetObjectData(SerializationInfo, StreamingContext)
_Exception::GetObjectData(SerializationInfo, StreamingContext)
| Exception | Condition |
|---|---|
| ArgumentNullException | The info object is nullptr. |
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 [<topic://cpconSerialization>].
[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. [Serializable] public ref 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( "ErrorDate" ); } public: 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" ); 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 = 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 ); myFileStream->Close(); 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 )); myFileStream->Close(); 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 ); } }
- SecurityCriticalAttribute
Requires full trust for the immediate caller. This member cannot be used by partially trusted or transparent code.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.