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 namespace System;
using namespace System::Reflection;
ref class App
{
};
int 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.
App::typeid->InvokeMember("DoSomething", BindingFlags::Static |
BindingFlags::InvokeMethod, nullptr, nullptr, nullptr);
}
catch (MissingMethodException^ ex)
{
// Show the user that the DoSomething method cannot be called.
Console::WriteLine("Unable to call the DoSomething method: {0}",
ex->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.
App::typeid->InvokeMember("AField", BindingFlags::Static |
BindingFlags::SetField, nullptr, nullptr, gcnew array<Object^>{5});
}
catch (MissingFieldException^ ex)
{
// Show the user that the AField field cannot be accessed.
Console::WriteLine("Unable to access the AField field: {0}",
ex->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.
App::typeid->InvokeMember("AnotherField", BindingFlags::Static |
BindingFlags::GetField, nullptr, nullptr, nullptr);
}
catch (MissingMemberException^ ex)
{
// 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}",
ex->Message);
}
}
// This code 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.
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 建構函式。
未指定載入靜態參考方法的確切時機。 這個例外狀況可能會在參考遺漏方法的方法開始執行之前擲回。
注意
.NET for Windows 市集應用程式 或 可攜式類別庫,但某些成員會擲回此例外狀況。 若要攔截該案例中的例外狀況,請改為撰寫 MissingMemberException 的 catch
語句。
建構函式
MissingMethodException() |
初始化 MissingMethodException 類別的新實例。 |
MissingMethodException(SerializationInfo, StreamingContext) |
已淘汰.
使用串行化數據,初始化 MissingMethodException 類別的新實例。 |
MissingMethodException(String, Exception) |
使用指定的錯誤訊息和造成這個例外狀況的內部例外狀況參考,初始化 MissingMethodException 類別的新實例。 |
MissingMethodException(String, String) |
使用指定的類別名稱和方法名稱,初始化 MissingMethodException 類別的新實例。 |
MissingMethodException(String) |
使用指定的錯誤訊息,初始化 MissingMethodException 類別的新實例。 |
欄位
ClassName |
保留遺漏成員的類別名稱。 (繼承來源 MissingMemberException) |
MemberName |
保留遺漏成員的名稱。 (繼承來源 MissingMemberException) |
Signature |
保留遺漏成員的簽章。 (繼承來源 MissingMemberException) |
屬性
Data |
取得索引鍵/值組的集合,提供例外狀況的其他使用者定義資訊。 (繼承來源 Exception) |
HelpLink |
取得或設定與這個例外狀況相關聯的說明檔連結。 (繼承來源 Exception) |
HResult |
取得或設定 HRESULT,這是指派給特定例外狀況的編碼數值。 (繼承來源 Exception) |
InnerException |
取得造成目前例外狀況的 Exception 實例。 (繼承來源 Exception) |
Message |
取得文字字串,其中顯示類別名稱、方法名稱和遺漏方法的簽章。 這個屬性是唯讀的。 |
Source |
取得或設定造成錯誤之應用程式或物件的名稱。 (繼承來源 Exception) |
StackTrace |
取得呼叫堆疊上即時框架的字串表示。 (繼承來源 Exception) |
TargetSite |
取得擲回目前例外狀況的方法。 (繼承來源 Exception) |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetBaseException() |
在衍生類別中覆寫時,傳回 Exception,這是一或多個後續例外狀況的根本原因。 (繼承來源 Exception) |
GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
GetObjectData(SerializationInfo, StreamingContext) |
已淘汰.
使用類別名稱、成員名稱、遺漏成員的簽章,以及其他例外狀況資訊,設定 SerializationInfo 物件。 (繼承來源 MissingMemberException) |
GetType() |
取得目前實例的運行時間類型。 (繼承來源 Exception) |
MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
ToString() |
建立並傳回目前例外狀況的字串表示。 (繼承來源 Exception) |
事件
SerializeObjectState |
已淘汰.
發生於例外狀況串行化以建立例外狀況狀態物件,其中包含例外狀況的串行化數據。 (繼承來源 Exception) |