MissingMethodException 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
嘗試動態存取不存在的方法時所擲回的例外狀況。
public ref class MissingMethodException : MissingMemberException
public class MissingMethodException : MissingMemberException
[System.Serializable]
public class MissingMethodException : MissingMemberException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class MissingMethodException : MissingMemberException
type MissingMethodException = class
inherit MissingMemberException
type MissingMethodException = class
inherit MissingMemberException
interface ISerializable
[<System.Serializable>]
type MissingMethodException = class
inherit MissingMemberException
interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MissingMethodException = class
inherit MissingMemberException
interface ISerializable
Public Class MissingMethodException
Inherits MissingMemberException
- 繼承
- 繼承
-
MissingMethodException
- 屬性
- 實作
範例
此範例示範如果您嘗試使用反映來呼叫不存在的方法,並存取不存在的字段,會發生什麼事。 應用程式會藉由擷取 MissingMethodException、MissingFieldException和 MissingMemberException來復原。
using System;
using System.Reflection;
public class App
{
public static void Main()
{
try
{
// Attempt to call a static DoSomething method defined in the App class.
// However, because the App class does not define this method,
// a MissingMethodException is thrown.
typeof(App).InvokeMember("DoSomething", BindingFlags.Static |
BindingFlags.InvokeMethod, null, null, null);
}
catch (MissingMethodException e)
{
// Show the user that the DoSomething method cannot be called.
Console.WriteLine("Unable to call the DoSomething method: {0}", e.Message);
}
try
{
// Attempt to access a static AField field defined in the App class.
// However, because the App class does not define this field,
// a MissingFieldException is thrown.
typeof(App).InvokeMember("AField", BindingFlags.Static | BindingFlags.SetField,
null, null, new Object[] { 5 });
}
catch (MissingFieldException e)
{
// Show the user that the AField field cannot be accessed.
Console.WriteLine("Unable to access the AField field: {0}", e.Message);
}
try
{
// Attempt to access a static AnotherField field defined in the App class.
// However, because the App class does not define this field,
// a MissingFieldException is thrown.
typeof(App).InvokeMember("AnotherField", BindingFlags.Static |
BindingFlags.GetField, null, null, null);
}
catch (MissingMemberException e)
{
// Notice that this code is catching MissingMemberException which is the
// base class of MissingMethodException and MissingFieldException.
// Show the user that the AnotherField field cannot be accessed.
Console.WriteLine("Unable to access the AnotherField field: {0}", e.Message);
}
}
}
// This code example produces the following output:
//
// Unable to call the DoSomething method: Method 'App.DoSomething' not found.
// Unable to access the AField field: Field 'App.AField' not found.
// Unable to access the AnotherField field: Field 'App.AnotherField' not found.
open System
open System.Reflection
type App = class end
try
// Attempt to call a static DoSomething method defined in the App class.
// However, because the App class does not define this method,
// a MissingMethodException is thrown.
typeof<App>.InvokeMember("DoSomething", BindingFlags.Static ||| BindingFlags.InvokeMethod, null, null, null)
|> ignore
with :? MissingMethodException as e ->
// Show the user that the DoSomething method cannot be called.
printfn $"Unable to call the DoSomething method: {e.Message}"
try
// Attempt to access a static AField field defined in the App class.
// However, because the App class does not define this field,
// a MissingFieldException is thrown.
typeof<App>.InvokeMember("AField", BindingFlags.Static ||| BindingFlags.SetField, null, null, [| box 5 |])
|> ignore
with :? MissingFieldException as e ->
// Show the user that the AField field cannot be accessed.
printfn $"Unable to access the AField field: {e.Message}"
try
// Attempt to access a static AnotherField field defined in the App class.
// However, because the App class does not define this field,
// a MissingFieldException is thrown.
typeof<App>.InvokeMember("AnotherField", BindingFlags.Static ||| BindingFlags.GetField, null, null, null)
|> ignore
with :? MissingMemberException as e ->
// Notice that this code is catching MissingMemberException which is the
// base class of MissingMethodException and MissingFieldException.
// Show the user that the AnotherField field cannot be accessed.
printfn $"Unable to access the AnotherField field: {e.Message}"
// This code example produces the following output:
// Unable to call the DoSomething method: Method 'App.DoSomething' not found.
// Unable to access the AField field: Field 'App.AField' not found.
// Unable to access the AnotherField field: Field 'App.AnotherField' not found.
Imports System.Reflection
Public Class App
Public Shared Sub Main()
Try
' Attempt to call a static DoSomething method defined in the App class.
' However, because the App class does not define this method,
' a MissingMethodException is thrown.
GetType(App).InvokeMember("DoSomething", BindingFlags.Static Or BindingFlags.InvokeMethod, _
Nothing, Nothing, Nothing)
Catch e As MissingMethodException
' Show the user that the DoSomething method cannot be called.
Console.WriteLine("Unable to call the DoSomething method: {0}", e.Message)
End Try
Try
' Attempt to access a static AField field defined in the App class.
' However, because the App class does not define this field,
' a MissingFieldException is thrown.
GetType(App).InvokeMember("AField", BindingFlags.Static Or BindingFlags.SetField, _
Nothing, Nothing, New [Object]() {5})
Catch e As MissingFieldException
' Show the user that the AField field cannot be accessed.
Console.WriteLine("Unable to access the AField field: {0}", e.Message)
End Try
Try
' Attempt to access a static AnotherField field defined in the App class.
' However, because the App class does not define this field,
' a MissingFieldException is thrown.
GetType(App).InvokeMember("AnotherField", BindingFlags.Static Or BindingFlags.GetField, _
Nothing, Nothing, Nothing)
Catch e As MissingMemberException
' Notice that this code is catching MissingMemberException which is the
' base class of MissingMethodException and MissingFieldException.
' Show the user that the AnotherField field cannot be accessed.
Console.WriteLine("Unable to access the AnotherField field: {0}", e.Message)
End Try
End Sub
End Class
' This code example produces the following output:
'
' Unable to call the DoSomething method: Method 'App.DoSomething' not found.
' Unable to access the AField field: Field 'App.AField' not found.
' Unable to access the AnotherField field: Field 'App.AnotherField' not found.
備註
如果程式代碼嘗試存取類別不存在的方法,通常會產生編譯錯誤。 MissingMethodException 的設計訴求是處理嘗試動態存取其強名稱未參考之元件的已重新命名或刪除方法的情況。 當相依元件中的程式代碼嘗試存取已修改之元件中的遺漏方法時,會擲回 MissingMethodException。
MissingMethodException 使用具有值0x80131513的 HRESULT COR_E_MISSINGMETHOD。
如需查看 MissingMethodException 實例的初始屬性值列表,請參閱 MissingMethodException 的建構子。
未指定載入靜態參考方法的確切時機。 這個例外狀況可能會在參考遺漏方法的方法開始執行之前擲回。
Note
此例外未包含在 catch 代為 MissingMemberException 的陳述。
建構函式
| 名稱 | Description |
|---|---|
| MissingMethodException() |
初始化 MissingMethodException 類別的新執行個體。 |
| MissingMethodException(SerializationInfo, StreamingContext) |
已淘汰.
初始化一個新的類別實例 MissingMethodException ,並使用序列化資料。 |
| MissingMethodException(String, Exception) |
初始化類別的新實例 MissingMethodException ,並附上指定的錯誤訊息及導致該異常的內部例外的參考。 |
| MissingMethodException(String, String) |
使用指定的類別名稱和方法名稱,初始化 MissingMethodException 類別的新實例。 |
| MissingMethodException(String) |
使用指定的錯誤訊息,初始化 MissingMethodException 類別的新實例。 |
欄位
| 名稱 | Description |
|---|---|
| ClassName |
保留遺漏成員的類別名稱。 (繼承來源 MissingMemberException) |
| MemberName |
保留遺漏成員的名稱。 (繼承來源 MissingMemberException) |
| Signature |
保留遺漏成員的簽章。 (繼承來源 MissingMemberException) |
屬性
| 名稱 | Description |
|---|---|
| Data |
取得一組鍵值對,提供關於例外的額外使用者定義資訊。 (繼承來源 Exception) |
| HelpLink |
取得或設定與此例外相關的說明檔案連結。 (繼承來源 Exception) |
| HResult |
取得或設定 HRESULT,一個編碼的數值,指派給特定例外。 (繼承來源 Exception) |
| InnerException |
會取得 Exception 造成目前例外的實例。 (繼承來源 Exception) |
| Message |
取得文字字串,其中顯示類別名稱、方法名稱和遺漏方法的簽章。 這個屬性是唯讀的。 |
| Source |
取得或設定造成錯誤之應用程式或物件的名稱。 (繼承來源 Exception) |
| StackTrace |
會取得呼叫堆疊上即時框架的字串表示。 (繼承來源 Exception) |
| TargetSite |
會取得拋出當前例外的方法。 (繼承來源 Exception) |
方法
| 名稱 | Description |
|---|---|
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetBaseException() |
當在派生類別中被覆寫時,回傳 Exception 是一個或多個後續例外的根因。 (繼承來源 Exception) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetObjectData(SerializationInfo, StreamingContext) |
已淘汰.
使用類別名稱、成員名稱、遺漏成員的簽章,以及其他例外狀況資訊,設定 SerializationInfo 物件。 (繼承來源 MissingMemberException) |
| GetType() |
取得目前實例的執行時型態。 (繼承來源 Exception) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| ToString() |
建立並回傳當前例外的字串表示。 (繼承來源 Exception) |
事件
| 名稱 | Description |
|---|---|
| SerializeObjectState |
已淘汰.
當例外被序列化以建立包含該例外序列化資料的例外狀態物件時,會發生這種情況。 (繼承來源 Exception) |