通过


TypeLoadException 构造函数

定义

初始化 TypeLoadException 类的新实例。

重载

名称 说明
TypeLoadException()

初始化 TypeLoadException 类的新实例。

TypeLoadException(String)

使用指定的错误消息初始化类的新实例 TypeLoadException

TypeLoadException(SerializationInfo, StreamingContext)
已过时.

使用序列化数据初始化类的新实例 TypeLoadException

TypeLoadException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用初始化类的新实例 TypeLoadException

TypeLoadException()

Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs

初始化 TypeLoadException 类的新实例。

public:
 TypeLoadException();
public TypeLoadException();
Public Sub New ()

注解

此构造函数将 Message 新实例的属性初始化为系统提供的消息,该消息描述错误,例如“加载类型时发生了失败”。此消息将考虑当前系统区域性。

下表显示了 TypeLoadException实例的初始属性值。

财产 价值
InnerException Null 引用(Nothing 在 Visual Basic 中)。
Message 本地化的错误消息字符串。

适用于

TypeLoadException(String)

Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs

使用指定的错误消息初始化类的新实例 TypeLoadException

public:
 TypeLoadException(System::String ^ message);
public TypeLoadException(string message);
public TypeLoadException(string? message);
new TypeLoadException : string -> TypeLoadException
Public Sub New (message As String)

参数

message
String

描述错误的消息。

示例

下面的代码示例演示构造 TypeLoadException(String) 函数。 它包含一个使用自定义消息生成 TypeLoadException 的方法,并向控制台显示错误消息。

using System;

public class Example
{
   public static void Main()
   {
      try {
         // Call a method that throws an exception.
         TypeLoadExceptionDemoClass.GenerateException();
      }
      catch (TypeLoadException e) {
         Console.WriteLine("TypeLoadException:\n   {0}", e.Message);
      }
   }
}

class TypeLoadExceptionDemoClass
{
   public static bool GenerateException()
   {
      // Throw a TypeLoadException with a custom defined message.
      throw new TypeLoadException("This is a custom TypeLoadException error message.");
   }
}
// The example displays the following output:
//       TypeLoadException:
//          This is a custom TypeLoadException error message.
Public Class Example
   Public Shared Sub Main()
      Try
         ' Call a method that throws an exception.
         TypeLoadExceptionDemoClass.GenerateException()
      Catch e As TypeLoadException
         Console.WriteLine("TypeLoadException:{0}   {1}", vbCrLf, e.Message)
      End Try
   End Sub 
End Class 

Class TypeLoadExceptionDemoClass
   Public Shared Function GenerateException() As Boolean
      ' Throw a TypeLoadException with a custom message.
      Throw New TypeLoadException("This is a custom TypeLoadException error message.")
   End Function 
End Class 
' The example displays the following output:
'       TypeLoadException:
'          This is a custom TypeLoadException error message.

注解

参数的内容 message 应该对用户理解。 此构造函数的调用方必须确保此字符串已本地化为当前系统区域性。

下表显示了 TypeLoadException实例的初始属性值。

财产 价值
InnerException Null 引用(Nothing 在 Visual Basic 中)。
Message 错误消息字符串。

适用于

TypeLoadException(SerializationInfo, StreamingContext)

Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs

注意

This API supports obsolete formatter-based serialization. It should not be called or extended by application code.

使用序列化数据初始化类的新实例 TypeLoadException

protected:
 TypeLoadException(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
[System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
protected TypeLoadException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
protected TypeLoadException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
[<System.Obsolete("This API supports obsolete formatter-based serialization. It should not be called or extended by application code.", DiagnosticId="SYSLIB0051", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
new TypeLoadException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> TypeLoadException
new TypeLoadException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> TypeLoadException
Protected Sub New (info As SerializationInfo, context As StreamingContext)

参数

info
SerializationInfo

保存序列化对象数据的对象。

context
StreamingContext

有关源或目标的上下文信息。

属性

例外

对象 infonull.

示例

以下示例生成异常,并将异常数据序列化为文件,然后重新构造异常。 若要运行此代码示例,必须提供完全限定的程序集名称。 有关如何获取完全限定程序集名称的信息,请参阅 程序集名称


using System;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
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;

      try
      {
         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);
         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.
         myException = (MyTypeLoadException) myFormatter.Deserialize(myFileStream);
         myFileStream.Close();
         Console.WriteLine("Deserialized exception has ErrorDateTime = " + myException.ErrorDateTime);
      }
   }
}

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

[Serializable]
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");
   }

   public override void GetObjectData(SerializationInfo sInfo, StreamingContext sContext)
   {
      base.GetObjectData(sInfo, sContext);
      // Add a value to the Serialization information.
      sInfo.AddValue("ErrorDate", ErrorDateTime);
   }
}
Imports System.Reflection
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.Security.Permissions
Imports System.IO

Class GetObjectDataDemo

   Public Shared Sub Main()
      ' Get a reference to the assembly mscorlib.dll, which is always
      ' loaded. (System.String is defined in mscorlib.)
      Dim tString As Type = GetType(String)
      Dim mscorlib As [Assembly] = tString.Assembly

      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 = mscorlib.GetType("System.NonExistentType", True)
      Catch
         ' Serialize the exception to disk and reconstitute it.
         Dim ErrorDatetime as System.DateTime = DateTime.Now
         Console.WriteLine("A TypeLoadException has been raised.")

         ' Create MyTypeLoadException instance with current time.
         Dim myException As new MyTypeLoadException(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, myException)
         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.
         myException = CType(myFormatter.Deserialize(myFileStream), MyTypeLoadException)
         myFileStream.Close()
         Console.WriteLine("Deserialized exception has ErrorDateTime = " + myException.ErrorDateTime.ToString())
      End Try
   End Sub
End Class

' This class overrides the GetObjectData method and initializes
' its data with current time. 
<Serializable()> _
Public Class MyTypeLoadException
   Inherits TypeLoadException

   Private _errorDateTime As System.DateTime = DateTime.Now
   Public ReadOnly Property ErrorDateTime As DateTime
      Get
         Return _errorDateTime
      End Get
   End Property

   Public Sub New(myDateTime As DateTime)
      _errorDateTime = myDateTime
   End Sub

   Protected Sub New(sInfo As SerializationInfo, sContext As StreamingContext)
      MyBase.New(sInfo, sContext)
      ' Reconstitute the deserialized information into the instance.
      _errorDateTime = sInfo.GetDateTime("ErrorDate")
   End Sub

   ' GetObjectData overrides must always have a demand for SerializationFormatter.
   <SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter:=true)> _
   Public Overrides Sub GetObjectData(sInfo As SerializationInfo, sContext As StreamingContext)
      MyBase.GetObjectData(sInfo, sContext)
      ' Add a value to the Serialization information.
      sInfo.AddValue("ErrorDate", ErrorDateTime)
   End Sub

End Class

注解

在反序列化期间调用此构造函数以重新构造通过流传输的异常对象。 有关详细信息,请参阅 XML 和 SOAP 序列化

另请参阅

适用于

TypeLoadException(String, Exception)

Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs
Source:
TypeLoadException.cs

使用指定的错误消息和对作为此异常原因的内部异常的引用初始化类的新实例 TypeLoadException

public:
 TypeLoadException(System::String ^ message, Exception ^ inner);
public TypeLoadException(string message, Exception inner);
public TypeLoadException(string? message, Exception? inner);
new TypeLoadException : string * Exception -> TypeLoadException
Public Sub New (message As String, inner As Exception)

参数

message
String

说明异常原因的错误消息。

inner
Exception

是当前异常原因的异常。 如果未 innernull 参数,则会在处理内部异常的 catch 块中引发当前异常。

示例

下面的代码示例演示构造 TypeLoadException(String, Exception) 函数。 它包含一个方法,该方法生成一个 TypeLoadException、捕获该异常,并引发包含自定义消息的新 TypeLoadException 消息,包括原始 TypeLoadException 异常作为内部异常。

using System;
using System.Runtime.InteropServices;

public class TypeLoadException_Constructor3
{
   public static void Main()
   {
      Console.WriteLine("Calling a method in a non-existent DLL which triggers a TypeLoadException.");
      try
      {
         TypeLoadExceptionDemoClass3.GenerateException();
      }
      catch (TypeLoadException e)
      {
         Console.WriteLine ("TypeLoadException: \n\tError Message = " + e.Message);
         Console.WriteLine ("TypeLoadException: \n\tInnerException Message = " + e.InnerException.Message );
      }
      catch (Exception e)
      {
         Console.WriteLine ("Exception: \n\tError Message = " + e.Message);
      }
   }
}

class TypeLoadExceptionDemoClass3
{
   // A call to this method will raise a TypeLoadException.
   [DllImport("NonExistentDLL.DLL", EntryPoint="MethodNotExists")]
   public static extern void NonExistentMethod();

   public static void GenerateException()
   {
      try
      {
         NonExistentMethod();
      }
      catch (TypeLoadException e)
      {
         // Rethrow exception with the exception as inner exception
         throw new TypeLoadException("This exception was raised due to a call to an invalid method.", e);
      }
   }
}
Imports System.Runtime.InteropServices

Public Class TypeLoadException_Constructor3
   Public Shared Sub Main()
      Console.WriteLine("Calling a method in a non-existent DLL which triggers a TypeLoadException.")
      Try
         TypeLoadExceptionDemoClass.GenerateException()
      Catch e As TypeLoadException
         Console.WriteLine(("TypeLoadException: " + ControlChars.Cr + ControlChars.Tab + "Error Message = " + e.Message))
         Console.WriteLine(("TypeLoadException: " + ControlChars.Cr + ControlChars.Tab + "InnerException Message = " + e.InnerException.Message))
      Catch e As Exception
         Console.WriteLine(("Exception: " + ControlChars.Cr + ControlChars.Tab + "Error Message = " + e.Message))
      End Try
   End Sub
End Class

Class TypeLoadExceptionDemoClass
   ' A call to this method will raise a TypeLoadException.
   Public Declare Sub NonExistentMethod Lib "NonExistentDLL.DLL" Alias "MethodNotExists" ()

   Public Shared Sub GenerateException()
      Try
         NonExistentMethod()
      Catch e As TypeLoadException
         ' Rethrow exception with the exception as inner exception
         Throw New TypeLoadException("This exception was raised due to a call to an invalid method.", e)
      End Try
   End Sub
End Class

注解

作为上一个异常的直接结果引发的异常可以包含对属性中上一个异常的 InnerException 引用。 InnerException 属性返回传入构造函数的相同值,或者如果 null 属性未向构造函数提供内部异常值,则返回 InnerException

下表显示了 TypeLoadException实例的初始属性值。

财产 价值
InnerException 内部异常引用。
Message 错误消息字符串。

另请参阅

适用于