TypeLoadException.GetObjectData Method (SerializationInfo, StreamingContext)


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

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

public override void GetObjectData(
	SerializationInfo info,
	StreamingContext context


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;
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;

         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);

         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);
         Console.WriteLine("Deserialized exception has ErrorDateTime = " + myException.ErrorDateTime);

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

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);


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