TypeLoadException Constructor (SerializationInfo, StreamingContext)


Initializes a new instance of the TypeLoadException class with serialized data.

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

protected TypeLoadException(
	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.

This constructor is called during deserialization to reconstitute the exception object transmitted over a stream. For more information, see XML and SOAP Serialization.

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

.NET Framework
Available since 1.1
Return to top