TypeLoadException 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 TypeLoadException 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| TypeLoadException() |
初始化 TypeLoadException 類別的新執行個體。 |
| TypeLoadException(String) |
初始化類別的新實例 TypeLoadException 並指定錯誤訊息。 |
| TypeLoadException(SerializationInfo, StreamingContext) |
已淘汰.
使用串行化數據,初始化 TypeLoadException 類別的新實例。 |
| TypeLoadException(String, Exception) |
初始化類別的新實例 TypeLoadException ,並附上指定的錯誤訊息及導致該異常的內部例外的參考。 |
TypeLoadException()
初始化 TypeLoadException 類別的新執行個體。
public:
TypeLoadException();
public TypeLoadException();
Public Sub New ()
備註
此建構子將新實例的屬性初始 Message 化為系統提供的訊息,描述錯誤,例如「載入型別時發生故障」。此訊息考量了當前系統文化。
下表顯示了 的 TypeLoadException初始屬性值。
| 房產 | 價值 |
|---|---|
| InnerException | 一個空參考(Nothing Visual Basic 中的 Reference)。 |
| Message | 區域化錯誤訊息字串。 |
適用於
TypeLoadException(String)
初始化類別的新實例 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 | 一個空參考(Nothing Visual Basic 中的 Reference)。 |
| Message | 錯誤訊息字串。 |
適用於
TypeLoadException(SerializationInfo, StreamingContext)
警告
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
關於來源或目的地的上下文資訊。
- 屬性
例外狀況
物件 info 為 null。
範例
以下範例會產生一個例外,並將例外資料序列化為檔案,然後再重建該例外。 要執行此程式碼範例,您必須提供完全限定的組合名稱。 關於如何取得完全合格的議會名稱,請參閱議會名稱。
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)
初始化類別的新實例 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
該例外即為當前例外的原因。 若 inner 參數不 null為 ,則在處理內部異常的區塊中提出 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 | 錯誤訊息字串。 |