Type.GetMethod 方法

定義

取得目前 Type 的特定方法。

多載

GetMethod(String, Int32, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

使用指定的繫結限制式和指定的呼叫慣例來搜尋指定的方法,其參數符合指定的泛型參數計數、引數型別及修飾元。

GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

使用指定的繫結條件約束和指定的呼叫慣例,來搜尋指定的方法,而該方法的參數符合指定的引數類型和修飾詞。

GetMethod(String, Int32, BindingFlags, Binder, Type[], ParameterModifier[])

使用指定的繫結限制式來搜尋指定的方法,其參數符合指定的泛型參數計數、引數型別及修飾元。

GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])

使用指定的繫結條件約束搜尋指定的方法,而該方法的參數符合指定的引數類型和修飾詞。

GetMethod(String, Int32, Type[], ParameterModifier[])

搜尋指定的公用方法,其參數符合指定的泛型參數計數、引數型別及修飾元。

GetMethod(String, Int32, BindingFlags, Type[])
GetMethod(String, BindingFlags)

使用指定的繫結條件約束搜尋指定的方法。

GetMethod(String, BindingFlags, Type[])

使用指定的系結條件約束,搜尋指定的方法,其參數符合指定的自變數類型。

GetMethod(String, Int32, Type[])

搜尋指定的公用方法,其參數符合指定的泛型參數計數和引數型別。

GetMethod(String, Type[])

搜尋指定的公用方法,其參數符合指定的引數類型。

GetMethod(String)

搜尋具有指定名稱的公用方法。

GetMethod(String, Type[], ParameterModifier[])

搜尋指定的公用方法,其參數符合指定的引數類型和修飾詞。

GetMethod(String, Int32, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

使用指定的繫結限制式和指定的呼叫慣例來搜尋指定的方法,其參數符合指定的泛型參數計數、引數型別及修飾元。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.MethodInfo? GetMethod (string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, bindingAttr As BindingFlags, binder As Binder, callConvention As CallingConventions, types As Type(), modifiers As ParameterModifier()) As MethodInfo

參數

name
String

字串,包含要取得的公用方法名稱。

genericParameterCount
Int32

方法的泛型型別參數數目。

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回 nullDefault

binder
Binder

定義一組屬性並啟用繫結的物件,可包含多載方法的選擇、引數類型的強制,以及透過反映的成員引動過程。

-或-

Null 參考 (在 Visual Basic 中為Nothing ),可使用 DefaultBinder

callConvention
CallingConventions

物件,其指定一組所要使用的規則,而這些規則是關於引數的順序和配置、如何傳遞傳回值、引數使用哪些暫存器以及如何清除堆疊。

types
Type[]

Type 物件的陣列,代表所要取得之方法的參數數目、順序和類型。

-或-

Type 物件的空陣列 (由 EmptyTypes 欄位提供),可取得不採用參數的方法。

modifiers
ParameterModifier[]

ParameterModifier 物件的陣列,代表在 types 陣列中與對應項目關聯的屬性。 只用於透過 COM Interop 進行呼叫以及只處理以傳址方式傳遞的參數。 預設的繫結器不會處理這個參數。

傳回

代表方法的物件,其符合指定的泛型參數計數、引數型別、修飾元、繫結限制式及呼叫慣例 (如果有的話);否則為 null

例外狀況

namenull

-或-

typesnull

-或-

types 陣列中的元素之一是 null

genericParameterCount 為負。

適用於

GetMethod(String, BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

使用指定的繫結條件約束和指定的呼叫慣例,來搜尋指定的方法,而該方法的參數符合指定的引數類型和修飾詞。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.MethodInfo? GetMethod (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, binder As Binder, callConvention As CallingConventions, types As Type(), modifiers As ParameterModifier()) As MethodInfo

參數

name
String

字串,包含要取得的方法名稱。

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回 nullDefault

binder
Binder

定義一組屬性並啟用繫結的物件,可包含多載方法的選擇、引數類型的強制,以及透過反映的成員引動過程。

-或-

Null 參考 (在 Visual Basic 中為Nothing ),可使用 DefaultBinder

callConvention
CallingConventions

物件,其指定一組所要使用的規則,而這些規則是關於引數的順序和配置、如何傳遞傳回值、引數使用哪些暫存器以及如何清除堆疊。

types
Type[]

Type 物件的陣列,代表所要取得之方法的參數數目、順序和類型。

-或-

Type 物件的空陣列 (由 EmptyTypes 欄位提供),可取得不採用參數的方法。

modifiers
ParameterModifier[]

ParameterModifier 物件的陣列,代表在 types 陣列中與對應項目關聯的屬性。 只用於透過 COM Interop 進行呼叫以及只處理以傳址方式傳遞的參數。 預設的繫結器不會處理這個參數。

傳回

物件,代表符合指定之需求的方法 (如有找到);否則為 null

實作

例外狀況

找到一個以上的方法,其具有指定名稱,且符合指定繫結條件約束。

namenull

-或-

typesnull

-或-

types 的其中一個項目為 null

types 是多維的。

-或-

modifiers 是多維的。

範例

下列範例會尋找的特定多載 MethodA,指定系結條件約束、呼叫慣例,以及各種自變數類型。

注意

Visual C# 2005 範例需要 /unsafe 編譯程序選項。

using namespace System;
using namespace System::Reflection;

public ref class Program
{

public:
    // Methods to get:

    void MethodA(int i, int j) { }

    void MethodA(array<int>^ iarry) { }

    void MethodA(double *ip) { }

    // Method that takes a managed reference paramter.
    void MethodA(int% r) {}
};

int main()
{
    MethodInfo^ mInfo;


    // Get MethodA(int i, int j)
    mInfo = Program::typeid->GetMethod("MethodA",
        BindingFlags::Public | BindingFlags::Instance,
        nullptr,
        CallingConventions::Any,
        gcnew array<Type^> {int::typeid, int::typeid},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get  MethodA(array<int>^ iarry)
    mInfo = Program::typeid->GetMethod("MethodA",
        BindingFlags::Public | BindingFlags::Instance,
        nullptr,
        CallingConventions::Any,
        gcnew array<Type^> {int::typeid->MakeArrayType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(double *ip)
    mInfo = Program::typeid->GetMethod("MethodA",
        BindingFlags::Public | BindingFlags::Instance,
        nullptr,
        CallingConventions::Any,
        gcnew array<Type^> {double::typeid->MakePointerType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(int% r)
    mInfo = Program::typeid->GetMethod("MethodA",
        BindingFlags::Public | BindingFlags::Instance,
        nullptr,
        CallingConventions::Any,
        gcnew array<Type^> {int::typeid->MakeByRefType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

}

using System;
using System.Reflection;

class Program3
{
    // Methods to get:

    public void MethodA(int i, int j) { }

    public void MethodA(int[] i) { }

    public unsafe void MethodA(int* i) { }

    public void MethodA(ref int r) {}

    // Method that takes an out parameter:
    public void MethodA(int i, out int o) { o = 100;}

  static void Main(string[] args)
  {
    MethodInfo mInfo;

    // Get MethodA(int i, int j)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int), typeof(int) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int[]) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int).MakePointerType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        CallingConventions.Any,
        new Type[] { typeof(int), typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);
  }
}
open System.Reflection

type Program() =
    // Methods to get:
    member _.MethodA(i: int, j: int) = ()

    member _.MethodA(i: int[]) = ()
    
    member _.MethodA(i: int nativeptr) = ()

    member _.MethodA(r: int byref) = ()

    // Method that takes an outref parameter:
    member _.MethodA(i: int, o: int outref) = o <- 100

do
    // Get MethodA(int i, int j)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>; typeof<int> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int[] i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int[]> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int* i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>.MakePointerType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(ref int r)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int i, out int o)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance, null, CallingConventions.Any, [| typeof<int>; typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {mInfo}"

Imports System.Reflection
Imports System.Runtime.InteropServices

Class Program

    ' Methods to get:
    
    Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)

    End Sub

    Public Overloads Sub MethodA(ByVal i() As Integer)

    End Sub

    Public Overloads Sub MethodA(ByRef r As Integer)

    End Sub

    ' Method that takes an integer and an out parameter. Note that an
    ' Imports reference is needed to System.Runtime.InteropServices
    ' for the <OutAttribute>, which can be shortened to <Out>.
    Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        o = 100
    End Sub

    Public Shared Sub Main(ByVal args() As String)
        Dim mInfo As MethodInfo

        ' Get MethodA(ByVal i As Integer, ByVal l As Long)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            CallingConventions.Any, _
            New Type() {GetType(System.Int32), _
            GetType(System.Int64)}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get  MethodA(ByVal i() As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            CallingConventions.Any, _
            New Type() {GetType(System.Int32())}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
        BindingFlags.Public Or BindingFlags.Instance, _
        Nothing, _
        CallingConventions.Any, _
        New Type() {GetType(System.Int32).MakeByRefType}, _
        Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
        BindingFlags.Public Or BindingFlags.Instance, _
        Nothing, _
        CallingConventions.Any, _
        New Type() {GetType(System.Int32), GetType(System.Int32).MakeByRefType}, _
        Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

備註

雖然預設系結器不會處理 ParameterModifier 參數 (modifiers) ,但您可以使用抽象 System.Reflection.Binder 類來撰寫會處理 modifiers的自定義系結器。 ParameterModifier 只有在透過 COM Interop 呼叫時,才會使用,而且只會處理以傳址方式傳遞的參數。

下表顯示反映類型時,方法會傳 GetXXX 回基類的成員。

成員類型 Static 非靜態
建構函式
欄位 可以。 欄位一律會依名稱與簽章隱藏。
事件 不適用 常見的類型系統規則是繼承與實作 屬性的方法相同。 反映會將屬性視為依名稱與簽章隱藏。 請參閱下面的附註 2。
方法 可以。 (虛擬和非虛擬) 的方法可以是依名稱隱藏或依名稱隱藏和簽章。
巢狀類型
屬性 不適用 常見的型別系統規則是繼承與實作 屬性的方法相同。 反映會將屬性視為 hide-by-name-and-signature。 請參閱下面的附註 2。
  1. 依名稱與簽章隱藏會考慮簽章的所有部分,包括自定義修飾詞、傳回類型、參數類型、sentinels 和 Unmanaged 呼叫慣例。 這是二進位比較。

  2. 針對反映,屬性和事件會依名稱與簽章隱藏。 如果您的屬性同時具有基類中的 get 和 set 存取子,但衍生類別只有 get 存取子,衍生類別屬性會隱藏基類屬性,而且您將無法在基類上存取 setter。

  3. 自訂屬性不是通用類型系統的一部分。

下列 BindingFlags 篩選旗標可用來定義要包含在搜尋中的方法:

  • 您必須指定 BindingFlags.InstanceBindingFlags.Static ,才能取得傳回。

  • 指定 BindingFlags.Public 以在搜尋中包含公用方法。

  • 指定 BindingFlags.NonPublic 以在搜尋中包含非公用方法 (,也就是私用、內部和受保護的方法) 。

  • 指定要 BindingFlags.FlattenHierarchypublic 階層中包含和 protected 靜態成員; private 繼承類別中的靜態成員不包含在內。

下列 BindingFlags 修飾詞旗標可用來變更搜尋的運作方式:

  • BindingFlags.IgnoreCase 忽略的大小 name寫。

  • BindingFlags.DeclaredOnly 只搜尋 在上 Type宣告的方法,而不是只繼承的方法。

如需相關資訊,請參閱 System.Reflection.BindingFlags

注意

查閱建構函式和方法時,您無法省略參數。 您只能在叫用時省略參數。

如果目前的 Type 代表建構的泛型型別,這個方法會傳回 MethodInfo ,並將 型別參數取代為適當的型別自變數。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,則這個方法會搜尋類別條件約束的方法,如果沒有類別條件約束,則為的方法 Object

注意

針對泛型方法,請勿在 中包含 name型別自變數。 例如,C# 程式代碼 GetMember("MyMethod<int>") 會搜尋具有文字名稱 「MyMethod<int>」 的成員,而不是名為 MyMethod 且類型為 之泛型自變數 int的方法。

另請參閱

適用於

GetMethod(String, Int32, BindingFlags, Binder, Type[], ParameterModifier[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

使用指定的繫結限制式來搜尋指定的方法,其參數符合指定的泛型參數計數、引數型別及修飾元。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.MethodInfo? GetMethod (string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetMethod : string * int * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, bindingAttr As BindingFlags, binder As Binder, types As Type(), modifiers As ParameterModifier()) As MethodInfo

參數

name
String

字串,包含要取得的公用方法名稱。

genericParameterCount
Int32

方法的泛型型別參數數目。

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回 nullDefault

binder
Binder

定義一組屬性並啟用繫結的物件,可包含多載方法的選擇、引數類型的強制,以及透過反映的成員引動過程。

-或-

Null 參考 (在 Visual Basic 中為Nothing ),可使用 DefaultBinder

types
Type[]

Type 物件的陣列,代表所要取得之方法的參數數目、順序和類型。

-或-

Type 物件的空陣列 (由 EmptyTypes 欄位提供),可取得不採用參數的方法。

modifiers
ParameterModifier[]

ParameterModifier 物件的陣列,代表在 types 陣列中與對應項目關聯的屬性。 只用於透過 COM Interop 進行呼叫以及只處理以傳址方式傳遞的參數。 預設的繫結器不會處理這個參數。

傳回

代表方法的物件,其符合指定的泛型參數計數、引數型別、修飾元及繫結限制式 (如果有的話);否則為 null

例外狀況

namenull

-或-

typesnull

-或-

types 陣列中的元素之一是 null

genericParameterCount 為負。

適用於

GetMethod(String, BindingFlags, Binder, Type[], ParameterModifier[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

使用指定的繫結條件約束搜尋指定的方法,而該方法的參數符合指定的引數類型和修飾詞。

public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.MethodInfo? GetMethod (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder? binder, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
abstract member GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, binder As Binder, types As Type(), modifiers As ParameterModifier()) As MethodInfo

參數

name
String

字串,包含要取得的方法名稱。

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回 nullDefault

binder
Binder

定義一組屬性並啟用繫結的物件,可包含多載方法的選擇、引數類型的強制,以及透過反映的成員引動過程。

-或-

Null 參考 (在 Visual Basic 中為Nothing ),可使用 DefaultBinder

types
Type[]

Type 物件的陣列,代表所要取得之方法的參數數目、順序和類型。

-或-

Type 物件的空陣列 (由 EmptyTypes 欄位提供),可取得不採用參數的方法。

modifiers
ParameterModifier[]

ParameterModifier 物件的陣列,代表在 types 陣列中與對應項目關聯的屬性。 只用於透過 COM Interop 進行呼叫以及只處理以傳址方式傳遞的參數。 預設的繫結器不會處理這個參數。

傳回

物件,代表符合指定之需求的方法 (如有找到);否則為 null

實作

例外狀況

找到一個以上的方法,其具有指定名稱,且符合指定繫結條件約束。

namenull

-或-

typesnull

-或-

types 的其中一個項目為 null

types 是多維的。

-或-

modifiers 是多維的。

範例

下列範例會尋找的特定多載 MethodA,並指定系結條件約束和各種自變數類型。

注意

Visual C# 2005 範例需要 /unsafe 編譯程序選項。

using namespace System;
using namespace System::Reflection;

public ref class Program
{

public:
    // Methods to get:

    void MethodA(int i, int j) { }

    void MethodA(array<int>^ iarry) { }

    void MethodA(double *ip) { }

    // Method that takes a managed reference parameter.
    void MethodA(int% r) {}
};

int main()
{
    MethodInfo^ mInfo;


    // Get MethodA(int i, int j)
    mInfo = Program::typeid->GetMethod("MethodA",
        static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
        nullptr,
        gcnew array<Type^> {int::typeid, int::typeid},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get  MethodA(array<int>^ iarry)
    mInfo = Program::typeid->GetMethod("MethodA",
        static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
        nullptr,
        gcnew array<Type^> {int::typeid->MakeArrayType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(double *ip)
    mInfo = Program::typeid->GetMethod("MethodA",
        static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
        nullptr,
        gcnew array<Type^> {double::typeid->MakePointerType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(int% r)
    mInfo = Program::typeid->GetMethod("MethodA",
        static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance),
        nullptr,
        gcnew array<Type^> {int::typeid->MakeByRefType()},
        nullptr);
    Console::WriteLine("Found method: {0}", mInfo );
}

using System;
using System.Reflection;

class Program5
{
    // Methods to get:

    public void MethodA(int i, int j) { }

    public void MethodA(int[] i) { }

    public unsafe void MethodA(int* i) { }

    public void MethodA(ref int r) {}

    // Method that takes an out parameter.
    public void MethodA(int i, out int o) { o = 100; }

  static void Main(string[] args)
  {
    MethodInfo mInfo;

    // Get MethodA(int i, int j)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int), typeof(int) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int[]) },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int).MakePointerType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        BindingFlags.Public | BindingFlags.Instance,
        null,
        new Type[] { typeof(int), typeof(int).MakeByRefType() },
        null);
    Console.WriteLine("Found method: {0}", mInfo);
  }
}
open System
open System.Reflection

type Program() =
    // Methods to get:
    member _.MethodA(i: int, j: int) = ()

    member _.MethodA(i: int[]) = ()
    
    member _.MethodA(i: int nativeptr) = ()

    member _.MethodA(r: int byref) = ()

    // Method that takes an outref parameter:
    member _.MethodA(i: int, o: int outref) = o <- 100

do
    // Get MethodA(int i, int j)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>; typeof<int> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int[] i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int[]> |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int* i)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>.MakePointerType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(ref int r)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {mInfo}"

    // Get MethodA(int i, out int o)
    let mInfo = 
        typeof<Program>.GetMethod("MethodA",BindingFlags.Public ||| BindingFlags.Instance, null, [| typeof<int>; typeof<int>.MakeByRefType() |], null)
    printfn $"Found method: {mInfo}"

Imports System.Reflection
Imports System.Runtime.InteropServices

Class Program

    ' Methods to get:
    
    Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)

    End Sub

    Public Overloads Sub MethodA(ByVal i() As Integer)

    End Sub

    Public Overloads Sub MethodA(ByRef r As Integer)

    End Sub

    ' Method that takes an out parameter. Note that an Imports
    ' reference is needed to System.Runtime.InteropServices 
    ' for the <OutAttribute>, which can be shortened to <Out>.
    Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        o = 100
    End Sub

    Public Shared Sub Main(ByVal args() As String)
        Dim mInfo As MethodInfo

        ' Get MethodA(ByVal i As Integer, ByVal l As Long)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32), _
            GetType(System.Int64)}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByVal i() As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32())}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32).MakeByRefType}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance, _
            Nothing, _
            New Type() {GetType(System.Int32), GetType(System.Int32).MakeByRefType}, _
            Nothing)
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

備註

雖然預設系結器不會處理 ParameterModifier 參數 (modifiers) ,但您可以使用抽象 System.Reflection.Binder 類來撰寫會處理 modifiers的自定義系結器。 ParameterModifier 只有在透過 COM Interop 呼叫時,才會使用,而且只會處理以傳址方式傳遞的參數。

下列 BindingFlags 篩選旗標可用來定義要包含在搜尋中的方法:

  • 您必須指定 BindingFlags.InstanceBindingFlags.Static ,才能取得傳回。

  • 指定 BindingFlags.Public 以在搜尋中包含公用方法。

  • 指定 BindingFlags.NonPublic 以在搜尋中包含非公用方法 (,也就是私用、內部和受保護的方法) 。

  • 指定要 BindingFlags.FlattenHierarchypublic 階層中包含和 protected 靜態成員; private 繼承類別中的靜態成員不包含在內。

下列 BindingFlags 修飾詞旗標可用來變更搜尋的運作方式:

  • BindingFlags.IgnoreCase 忽略的大小 name寫。

  • BindingFlags.DeclaredOnly 只搜尋 在上 Type宣告的方法,而不是只繼承的方法。

如需相關資訊,請參閱 System.Reflection.BindingFlags

注意

查閱建構函式和方法時,您無法省略參數。 您只能在叫用時省略參數。

如果目前的 Type 代表建構的泛型型別,這個方法會傳回 MethodInfo ,並將 型別參數取代為適當的型別自變數。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,則這個方法會搜尋類別條件約束的方法,如果沒有類別條件約束,則為的方法 Object

注意

針對泛型方法,請勿在 中包含 name型別自變數。 例如,C# 程式代碼 GetMember("MyMethod<int>") 會搜尋具有文字名稱 「MyMethod<int>」 的成員,而不是名為 MyMethod 且類型為 之泛型自變數 int的方法。

另請參閱

適用於

GetMethod(String, Int32, Type[], ParameterModifier[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

搜尋指定的公用方法,其參數符合指定的泛型參數計數、引數型別及修飾元。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.MethodInfo? GetMethod (string name, int genericParameterCount, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, int genericParameterCount, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetMethod : string * int * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, types As Type(), modifiers As ParameterModifier()) As MethodInfo

參數

name
String

字串,包含要取得的公用方法名稱。

genericParameterCount
Int32

方法的泛型型別參數數目。

types
Type[]

Type 物件的陣列,代表所要取得之方法的參數數目、順序和類型。

-或-

Type 物件的空陣列 (由 EmptyTypes 欄位提供),可取得不採用參數的方法。

modifiers
ParameterModifier[]

ParameterModifier 物件的陣列,代表在 types 陣列中與對應項目關聯的屬性。 只用於透過 COM Interop 進行呼叫以及只處理以傳址方式傳遞的參數。 預設的繫結器不會處理這個參數。

傳回

代表公用方法的物件,其符合指定的泛型參數計數、引數型別及修飾元 (如果有的話);否則為 null

例外狀況

namenull

-或-

typesnull

-或-

types 陣列中的元素之一是 null

genericParameterCount 為負。

適用於

GetMethod(String, Int32, BindingFlags, Type[])

Source:
Type.cs
public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
public System.Reflection.MethodInfo? GetMethod (string name, int genericParameterCount, System.Reflection.BindingFlags bindingAttr, Type[] types);
member this.GetMethod : string * int * System.Reflection.BindingFlags * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, bindingAttr As BindingFlags, types As Type()) As MethodInfo

參數

name
String
genericParameterCount
Int32
bindingAttr
BindingFlags
types
Type[]

傳回

適用於

GetMethod(String, BindingFlags)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

使用指定的繫結條件約束搜尋指定的方法。

public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
public System.Reflection.MethodInfo? GetMethod (string name, System.Reflection.BindingFlags bindingAttr);
public System.Reflection.MethodInfo GetMethod (string name, System.Reflection.BindingFlags bindingAttr);
abstract member GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
override this.GetMethod : string * System.Reflection.BindingFlags -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags) As MethodInfo

參數

name
String

字串,包含要取得的方法名稱。

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。

-或-

要傳回 nullDefault

傳回

物件,代表符合指定之需求的方法 (如有找到);否則為 null

實作

例外狀況

找到一個以上的方法,其具有指定名稱,且符合指定繫結條件約束。

namenull

範例

下列範例會取得符合指定系結旗標的方法。

using namespace System;
using namespace System::Reflection;
public ref class Program
{

    public:

        // Method to get:
        void MethodA() { }

    };

    int main()
    {

        // Get MethodA()
        MethodInfo^ mInfo = Program::typeid->GetMethod("MethodA",
            static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance));
        Console::WriteLine("Found method: {0}", mInfo );

    }
using System;
using System.Reflection;

class Program2
{
    // Method to get:
    public void MethodA() { }

    static void Main(string[] args)
    {
        // Get MethodA()
        MethodInfo mInfo = typeof(Program).GetMethod("MethodA",
            BindingFlags.Public | BindingFlags.Instance);
        Console.WriteLine("Found method: {0}", mInfo);
    }
}
open System.Reflection

type Program() =
    // Method to get:
    member _.MethodA() = ()

// Get MethodA()
let mInfo = typeof<Program>.GetMethod("MethodA", BindingFlags.Public ||| BindingFlags.Instance)
printfn $"Found method: {mInfo}"

Imports System.Reflection

Class Program

    ' Method to get:
    Public Sub MethodA()
    End Sub


    Public Shared Sub Main(ByVal args() As String)

        ' Get MethodA()
        Dim mInfo As MethodInfo = GetType(Program).GetMethod("MethodA", _
            BindingFlags.Public Or BindingFlags.Instance)
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

備註

下列 BindingFlags 篩選旗標可用來定義要包含在搜尋中的方法:

下列 BindingFlags 修飾詞旗標可用來變更搜尋的運作方式:

如需相關資訊,請參閱 System.Reflection.BindingFlags

如果方法已多載,且多個多載符合 自變數所 bindingAttr 指定的條件約束,則方法會 AmbiguousMatchException 擲回例外狀況。 在下列範例中,會擲回例外狀況,因為:

  • TestClass 型別有兩個方法的 DisplayValue 公用實例多載, DisplayValue(String) 以及 DisplayValue(String, Object[])

  • TestClass 型別有兩個 方法的 Equals 公用實例多載,其中一個是繼承自 ObjectEquals(TestClass)Equals(Object)

using System;
using System.Reflection;

public class TestClass
{
    public void DisplayValue(String s)
    {
        Console.WriteLine(s);
    }

    public void DisplayValue(String s, params Object[] values)
    {
        Console.WriteLine(s, values);
    }

    public static bool Equals(TestClass t1, TestClass t2)
    {
        return Object.ReferenceEquals(t1, t2);
    }

    public bool Equals(TestClass t)
    {
        return Object.ReferenceEquals(this, t);
    }
}

public class Example1
{
    public static void Main()
    {
        Type t = typeof(TestClass);

        RetrieveMethod(t, "DisplayValue", BindingFlags.Public | BindingFlags.Instance);

        RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Instance);

        RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

        RetrieveMethod(t, "Equals", BindingFlags.Public | BindingFlags.Static);
    }

    private static void RetrieveMethod(Type t, String name, BindingFlags flags)
    {
        try
        {
            MethodInfo m = t.GetMethod(name, flags);
            if (m != null)
            {
                Console.Write("{0}.{1}(", t.Name, m.Name);
                ParameterInfo[] parms = m.GetParameters();
                for (int ctr = 0; ctr < parms.Length; ctr++)
                {
                    Console.Write(parms[ctr].ParameterType.Name);
                    if (ctr < parms.Length - 1)
                        Console.Write(", ");
                }
                Console.WriteLine(")");
            }
            else
            {
                Console.WriteLine("Method not found");
            }
        }
        catch (AmbiguousMatchException)
        {
            Console.WriteLine("The following duplicate matches were found:");
            MethodInfo[] methods = t.GetMethods(flags);
            foreach (var method in methods)
            {
                if (method.Name != name) continue;

                Console.Write("   {0}.{1}(", t.Name, method.Name);
                ParameterInfo[] parms = method.GetParameters();
                for (int ctr = 0; ctr < parms.Length; ctr++)
                {
                    Console.Write(parms[ctr].ParameterType.Name);
                    if (ctr < parms.Length - 1)
                        Console.Write(", ");
                }
                Console.WriteLine(")");
            }
        }
        Console.WriteLine();
    }
}
// The example displays the following output:
//       The following duplicate matches were found:
//          TestClass.DisplayValue(String)
//          TestClass.DisplayValue(String, Object[])
//       
//       The following duplicate matches were found:
//          TestClass.Equals(TestClass)
//          TestClass.Equals(Object)
//       
//       TestClass.Equals(TestClass)
//       
//       TestClass.Equals(TestClass, TestClass)
open System
open System.Reflection

type TestClass() =
    member _.DisplayValue(s) = 
        printfn $"%s{s}"

    member _.DisplayValue(s: string, [<ParamArray>]values: obj[]) =
        Console.WriteLine(s, values)

    member this.Equals(t: TestClass) =
        Object.ReferenceEquals(this, t)

    static member Equals(t1: TestClass, t2: TestClass) =
        Object.ReferenceEquals(t1, t2)

let retrieveMethod (t: Type) name (flags: BindingFlags) =
    try
        let m = t.GetMethod(name, flags)
        if m <> null then
            printf $"{t.Name}.{m.Name}("
            let parms = m.GetParameters()
            for i = 0 to parms.Length - 1 do
                printf $"{parms[i].ParameterType.Name}"
                if i < parms.Length - 1 then
                    printf ", "
            printfn ")"
        else
            printfn "Method not found"
    with :? AmbiguousMatchException ->
        printfn "The following duplicate matches were found:"
        let methods = t.GetMethods flags
        for method in methods do
            if method.Name = name then
                printf $"   {t.Name}.{method.Name}("
                let parms = method.GetParameters()
                for i = 0 to parms.Length - 1 do
                    printf $"{parms[i].ParameterType.Name}"
                    if i < parms.Length - 1 then
                        printf ", "
                printfn ")"
    printfn ""

let t = typeof<TestClass>

retrieveMethod t "DisplayValue" (BindingFlags.Public ||| BindingFlags.Instance)

retrieveMethod t "Equals" (BindingFlags.Public ||| BindingFlags.Instance)

retrieveMethod t "Equals" (BindingFlags.Public ||| BindingFlags.Instance ||| BindingFlags.DeclaredOnly)

retrieveMethod t "Equals" (BindingFlags.Public ||| BindingFlags.Static)

// The example displays the following output:
//       The following duplicate matches were found:
//          TestClass.DisplayValue(String)
//          TestClass.DisplayValue(String, Object[])
//       
//       The following duplicate matches were found:
//          TestClass.Equals(TestClass)
//          TestClass.Equals(Object)
//       
//       TestClass.Equals(TestClass)
//       
//       TestClass.Equals(TestClass, TestClass)
Imports System.Reflection

Public Class TestClass
   Public Sub DisplayValue(s As String)
      Console.WriteLine(s)
   End Sub
   
   Public Sub DisplayValue(s As String, ParamArray values() As Object)
      Console.WriteLine(s, values)
   End Sub
   
   Public Overloads Shared Function Equals(t1 As TestClass, t2 As TestClass) As Boolean
      Return Object.ReferenceEquals(t1, t2)
   End Function
   
   Public Overloads Function Equals(t As TestClass) As Boolean
      Return Object.ReferenceEquals(Me, t)
   End Function          
End Class

Module Example
   Public Sub Main()
      Dim t As Type = GetType(TestClass)
      
      RetrieveMethod(t, "DisplayValue", BindingFlags.Public Or BindingFlags.Instance)

      RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Instance)
      
      RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
      
      RetrieveMethod(t, "Equals", BindingFlags.Public Or BindingFlags.Static)
   End Sub
   
   Public Sub RetrieveMethod(t As Type, name As String, flags As BindingFlags)
      Try
         Dim m As MethodInfo = t.GetMethod(name, flags)
         If m IsNot Nothing Then
            Console.Write("{0}.{1}(", t.Name, m.Name)
           Dim parms() As ParameterInfo = m.GetParameters()
            For ctr As Integer = 0 To parms.Length - 1
               Console.Write(parms(ctr).ParameterType.Name)
               if ctr < parms.Length - 1 Then 
                  Console.Write(", ")
               End If      
            Next
            Console.WriteLine(")")
         Else
            Console.WriteLine("Method not found")
         End If
      Catch e As AmbiguousMatchException
         Console.WriteLine("The following duplicate matches were found:")
         Dim methods() As MethodInfo = t.GetMethods(flags)
         For Each method In methods
            If method.Name <> name Then Continue For

            Console.Write("   {0}.{1}(", t.Name, method.Name)
            Dim parms() As ParameterInfo = method.GetParameters()
            For ctr As Integer = 0 To parms.Length - 1
               Console.Write(parms(ctr).ParameterType.Name)
               if ctr < parms.Length - 1 Then 
                  Console.Write(", ")
               End If      
            Next
            Console.WriteLine(")")
         Next 
      End Try         
      Console.WriteLine()
   End Sub
End Module
' The example displays the following output:
'       The following duplicate matches were found:
'          TestClass.DisplayValue(String)
'          TestClass.DisplayValue(String, Object[])
'       
'       The following duplicate matches were found:
'          TestClass.Equals(TestClass)
'          TestClass.Equals(Object)
'       
'       TestClass.Equals(TestClass)
'       
'       TestClass.Equals(TestClass, TestClass)

您可以執行下列其中一項來擷取特定方法:

  • 變更系結條件約束。 在上述範例中,嘗試擷取型別所宣告且未繼承的公用實例 Equals 方法會成功擷 Equals(TestClass)取 。

  • 呼叫 方法的多 GetMethod 載,其中包含 types 定義方法參數類型的參數。

  • GetMethods(BindingFlags)呼叫 方法,以擷取陣列,其中包含屬於具有指定系結屬性之型別的所有方法。 然後,您可以逐一查看它,以識別名為 name的重複方法。 在上述範例的例外狀況處理程式 AmbiguousMatchException 中會說明這個方法。

如果目前的 Type 代表建構的泛型型別,這個方法會傳回 MethodInfo ,並將 型別參數取代為適當的型別自變數。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,則這個方法會搜尋類別條件約束的方法,如果沒有類別條件約束,則為的方法 Object

注意

針對泛型方法,請勿在 中包含 name型別自變數。 例如,C# 程式代碼 GetMember("MyMethod<int>") 會搜尋具有文字名稱 「MyMethod<int>」 的成員,而不是名為 MyMethod 且類型為 之泛型自變數 int的方法。

另請參閱

適用於

GetMethod(String, BindingFlags, Type[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

使用指定的系結條件約束搜尋指定的方法,其參數符合指定的自變數類型。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, System::Reflection::BindingFlags bindingAttr, cli::array <Type ^> ^ types);
public System.Reflection.MethodInfo? GetMethod (string name, System.Reflection.BindingFlags bindingAttr, Type[] types);
member this.GetMethod : string * System.Reflection.BindingFlags * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, bindingAttr As BindingFlags, types As Type()) As MethodInfo

參數

name
String

字串,包含要取得的方法名稱。

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。 -或- 預設傳回 null

types
Type[]

Type 物件的陣列,代表所要取得之方法的參數數目、順序和類型。 -或- 物件的空陣列 Type (,如欄位) 所提供 EmptyTypes ,以取得不採用任何參數的方法。

傳回

物件,代表符合指定之需求的方法 (如有找到);否則為 null

適用於

GetMethod(String, Int32, Type[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

搜尋指定的公用方法,其參數符合指定的泛型參數計數和引數型別。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, int genericParameterCount, cli::array <Type ^> ^ types);
public System.Reflection.MethodInfo? GetMethod (string name, int genericParameterCount, Type[] types);
public System.Reflection.MethodInfo GetMethod (string name, int genericParameterCount, Type[] types);
member this.GetMethod : string * int * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, genericParameterCount As Integer, types As Type()) As MethodInfo

參數

name
String

字串,包含要取得的公用方法名稱。

genericParameterCount
Int32

方法的泛型型別參數數目。

types
Type[]

Type 物件的陣列,代表所要取得之方法的參數數目、順序和類型。

-或-

Type 物件的空陣列 (由 EmptyTypes 欄位提供),可取得不採用參數的方法。

傳回

代表公用方法的物件,其參數符合指定的泛型參數計數和引數型別 (如果有的話);否則為 null

例外狀況

namenull

-或-

typesnull

-或-

types 陣列中的元素之一是 null

genericParameterCount 為負。

適用於

GetMethod(String, Type[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

搜尋指定的公用方法,其參數符合指定的引數類型。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types);
public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types);
public System.Reflection.MethodInfo? GetMethod (string name, Type[] types);
public System.Reflection.MethodInfo GetMethod (string name, Type[] types);
member this.GetMethod : string * Type[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * Type[] -> System.Reflection.MethodInfo
override this.GetMethod : string * Type[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, types As Type()) As MethodInfo

參數

name
String

字串,包含要取得的公用方法名稱。

types
Type[]

Type 物件的陣列,代表所要取得之方法的參數數目、順序和類型。

-或-

Type 物件的空陣列 (由 EmptyTypes 欄位提供),可取得不採用參數的方法。

傳回

代表其參數符合指定之引數類型的公用方法物件 (如有找到);否則為 null

實作

例外狀況

找到一個以上具有指定名稱及指定參數的方法。

namenull

-或-

typesnull

-或-

types 的其中一個項目為 null

types 是多維的。

範例

下列範例會尋找的特定多載 MethodA,並指定各種自變數類型。

注意

Visual C# 2005 範例需要 /unsafe 編譯程序選項。

using namespace System;
using namespace System::Reflection;

public ref class Program
{

public:
    // Methods to get:

    void MethodA(int i, int j) { }

    void MethodA(array<int>^ iarry) { }

    void MethodA(double *ip) { }

    // Method that takes a managed reference parameter.
    void MethodA(int% r) {}
};

int main()
{
    MethodInfo^ mInfo;


    // Get MethodA(int i, int j)
    mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {int::typeid,int::typeid});
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(array<int>^ iarry)
    mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {int::typeid->MakeArrayType()});
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(double *ip)
    mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {double::typeid->MakePointerType()});
    Console::WriteLine("Found method: {0}", mInfo );

    // Get MethodA(int% r)
    mInfo = Program::typeid->GetMethod("MethodA", gcnew array<Type^> {int::typeid->MakeByRefType()});
    // Display the method information.
    Console::WriteLine("Found method: {0}", mInfo );

}

using System;
using System.Reflection;

class Program4
{
    // Methods to get:

    public void MethodA(int i, int j) { }

    public void MethodA(int[] i) { }

    public unsafe void MethodA(int* i) { }

    public void MethodA(ref int r) {}

    // Method that takes an out parameter:
    public void MethodA(int i, out int o) { o = 100;}

  static void Main(string[] args)
  {
    MethodInfo mInfo;

    // Get MethodA(int i, int i)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int), typeof(int) });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int[] i)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int[]) });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int* i)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int).MakePointerType() });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(ref int r)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int).MakeByRefType() });
    Console.WriteLine("Found method: {0}", mInfo);

    // Get MethodA(int i, out int o)
    mInfo = typeof(Program).GetMethod("MethodA",
        new Type[] { typeof(int), typeof(int).MakeByRefType() });
    Console.WriteLine("Found method: {0}", mInfo);
  }
}
type Program() =
    // Methods to get:
    member _.MethodA(i: int, j: int) = ()

    member _.MethodA(i: int[]) = ()
    
    member _.MethodA(i: int nativeptr) = ()

    member _.MethodA(r: int byref) = ()

    // Method that takes an outref parameter:
    member _.MethodA(i: int, o: int outref) = o <- 100

do
    // member MethodA: i: int * j: int -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>; typeof<int> |])
    printfn $"Found method: {mInfo}"

    // member MethodA: i: int[] -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int[]> |])
    printfn $"Found method: {mInfo}"

    // member MethodA: i: nativeptr<int> -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>.MakePointerType() |])
    printfn $"Found method: {mInfo}"

    // member MethodA: r: byref<int> -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>.MakeByRefType() |])
    printfn $"Found method: {mInfo}"

    // member MethodA: i: int * o: outref<int> -> unit
    let mInfo = typeof<Program>.GetMethod("MethodA", [| typeof<int>; typeof<int>.MakeByRefType() |])
    printfn $"Found method: {mInfo}"

Imports System.Reflection
Imports System.Runtime.InteropServices

Class Program

    ' Methods to get:

    Public Overloads Sub MethodA(ByVal i As Integer, ByVal l As Long)

    End Sub

    Public Overloads Sub MethodA(ByVal i() As Integer)

    End Sub

    Public Overloads Sub MethodA(ByRef r As Integer)

    End Sub

    ' Method that takes an out parameter. Note that an Imports
    ' reference is needed to System.Runtime.InteropServices 
    ' for the <OutAttribute>, which can be shortened to <Out>.
    Public Overloads Sub MethodA(ByVal i As Integer, <Out()> ByRef o As Integer)
        o = 100
    End Sub

    Public Shared Sub Main(ByVal args() As String)
        Dim mInfo As MethodInfo

        ' Get MethodA(i As Integer i, l As Long)
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), GetType(Long)})
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get  MethodA(i As Integer())
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer())})
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer).MakeByRefType})
        Console.WriteLine("Found method: {0}", mInfo)

        ' Get MethodA(i As Integer, ByRef r As Integer)
        mInfo = GetType(Program).GetMethod("MethodA", New Type() {GetType(Integer), _
            GetType(Integer).MakeByRefType})
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

下列範例會 MethodInfo 擷取對象,這些物件代表 Add 類別) ArrayList (非泛型型別的方法、 (類別) (開啟的泛型型 List<T> 別,以及型別 (List(Of String) 封閉的泛型型別。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      // Get a Type object that represents a non-generic type.
      GetAddMethod(typeof(ArrayList));

      var list = new List<String>();
      // Get a Type object that represents a constructed generic type.
      Type closed = list.GetType();
      GetAddMethod(closed);
      
      // Get a Type object that represents an open generic type.
      Type open = typeof(List<>);
      GetAddMethod(open);
   }

   private static void GetAddMethod(Type typ)
   {
      MethodInfo method;
      // Determine if this is a generic type.
      if (typ.IsGenericType) {
         // Is it an open generic type?
         if (typ.ContainsGenericParameters)
            method = typ.GetMethod("Add", typ.GetGenericArguments());
         // Get closed generic type arguments.
         else
            method = typ.GetMethod("Add", typ.GenericTypeArguments);
      }
      // This is not a generic type.
      else {
         method = typ.GetMethod("Add", new Type[] { typeof(Object) } );
      }

      // Test if an Add method was found.
      if (method == null) { 
         Console.WriteLine("No Add method found.");
         return;
      }   
      
      Type t = method.ReflectedType;
      Console.Write("{0}.{1}.{2}(", t.Namespace, t.Name, method.Name);
      ParameterInfo[] parms = method.GetParameters();
      for (int ctr = 0; ctr < parms.Length; ctr++)
         Console.Write("{0}{1}", parms[ctr].ParameterType.Name, 
                       ctr < parms.Length - 1 ? ", " : "");

      Console.WriteLine(")");
   }   
}
// The example displays the following output:
//       System.Collections.ArrayList.Add(Object)
//       System.Collections.Generic.List`1.Add(String)
//       System.Collections.Generic.List`1.Add(T)
open System
open System.Collections

let getAddMethod (typ: Type) = 
    let method = 
        // Determine if this is a generic type.
        if typ.IsGenericType then
            // Is it an open generic type?
            if typ.ContainsGenericParameters then
                typ.GetMethod("Add", typ.GetGenericArguments())
            // Get closed generic type arguments.
            else
                typ.GetMethod("Add", typ.GenericTypeArguments)
        // This is not a generic type.
        else
            typ.GetMethod("Add", [| typeof<obj> |])

    // Test if an Add method was found.
    if method = null then
        printfn "No Add method found."
    else
        let t = method.ReflectedType
        printf $"{t.Namespace}.{t.Name}.{method.Name}("
        let parms = method.GetParameters()
        for i = 0 to parms.Length - 1 do
            printf $"""{parms[i].ParameterType.Name}{if i < parms.Length - 1 then ", " else ""}"""
        printfn ")"

// Get a Type object that represents a non-generic type.
getAddMethod typeof<ArrayList>

let list = ResizeArray<String>()
// Get a Type object that represents a constructed generic type.
let closed = list.GetType()
getAddMethod closed

// Get a Type object that represents an open generic type.
let opn = typeof<ResizeArray<_>>.GetGenericTypeDefinition()
getAddMethod opn

// The example displays the following output:
//       System.Collections.ArrayList.Add(Object)
//       System.Collections.Generic.List`1.Add(String)
//       System.Collections.Generic.List`1.Add(T)
Imports System.Collections
Imports System.Collections.Generic
Imports System.Reflection

Module Example
   Public Sub Main()
      ' Get a Type object that represents a non-generic type.
      GetAddMethod(GetType(ArrayList))
      
      Dim list As New List(Of String)()
      ' Get a Type object that represents a constructed generic type.
      Dim closed As Type = list.GetType()
      GetAddMethod(closed)
      
      ' Get a Type object that represents an open generic type.
      Dim open As Type = GetType(List(Of))
      GetAddMethod(open)
   End Sub
   
   Private Sub GetAddMethod(typ As Type)
      Dim method As MethodInfo
      ' Determine if this is a generic type.
      If typ.IsGenericType Then
         ' Is it an open generic type?
         If typ.ContainsGenericParameters Then
            method = typ.GetMethod("Add", typ.GetGenericArguments())
         ' Get closed generic type arguments.
         Else
            method = typ.GetMethod("Add", typ.GenericTypeArguments)
         End If
      ' This is not a generic type.
      Else
         method = typ.GetMethod("Add", { GetType(Object) } )
      End If
      ' Test if an Add method was found.
      If method Is Nothing Then 
         Console.WriteLine("No Add method found.")
         Exit Sub
      End If   

      Dim t As Type = method.ReflectedType
      Console.Write("{0}.{1}.{2}(", t.Namespace, t.Name, method.Name)
      Dim params() As ParameterInfo = method.GetParameters()
      For ctr As Integer = 0 To params.Length - 1
         Console.Write("{0}{1}", params(ctr).ParameterType.Name, 
                       If(ctr < params.Length - 1, ", ", ""))
      Next
      Console.WriteLine(")")
   End Sub
End Module
' The example displays the following output:
'       System.Collections.ArrayList.Add(Object)
'       System.Collections.Generic.List`1.Add(String)
'       System.Collections.Generic.List`1.Add(T)

此範例會 GetAddMethod 定義擷取適當 MethodInfo 物件的方法。 若要提供 types 開啟泛型類型的自變數,它會呼叫 Type.GetGenericArguments 方法。 若要提供 types 封閉式泛型類型的自變數,它會擷取 屬性的值 Type.GenericTypeArguments

備註

搜尋 name 會區分大小寫。 搜尋包含公用靜態和公用實例方法。

注意

查閱建構函式和方法時,您無法省略參數。 您只能在叫用時省略參數。

如果目前的 Type 代表建構的泛型型別,這個方法會傳回 MethodInfo ,並將 型別參數取代為適當的型別自變數。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,則這個方法會搜尋類別條件約束的方法,如果沒有類別條件約束,則為的方法 Object

注意

參數 name 不能包含類型自變數。 例如,C# 程式代碼 GetMethod("MyGenericMethod<int>") 會搜尋具有文字名稱 「MyGenericMethod<int>的方法,而不是名為 MyGenericMethod 且類型為 之泛型自變數 int的方法。 請改用 GetMethod("MyGenericMethod") 陣列中的 types 適當參數。

另請參閱

適用於

GetMethod(String)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

搜尋具有指定名稱的公用方法。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name);
public System.Reflection.MethodInfo? GetMethod (string name);
public System.Reflection.MethodInfo GetMethod (string name);
member this.GetMethod : string -> System.Reflection.MethodInfo
abstract member GetMethod : string -> System.Reflection.MethodInfo
override this.GetMethod : string -> System.Reflection.MethodInfo
Public Function GetMethod (name As String) As MethodInfo

參數

name
String

字串,包含要取得的公用方法名稱。

傳回

物件,代表具有指定之名稱的公用方法 (如有找到);否則為 null

實作

例外狀況

找到一個以上具有指定名稱的方法。

namenull

範例

下列範例會取得名為 MethodA的方法。

using namespace System;
using namespace System::Reflection;
public ref class Program
{

    public:

        // Method to get:
        void MethodA() { }

    };

    int main()
    {

        // Get MethodA()
        MethodInfo^ mInfo = Program::typeid->GetMethod("MethodA");
        Console::WriteLine("Found method: {0}", mInfo );

    }

using System;
using System.Reflection;

class Program
{

    // Method to get:
    public void MethodA() { }

    static void Main(string[] args)
    {

        // Get MethodA()
        MethodInfo mInfo = typeof(Program).GetMethod("MethodA");
        Console.WriteLine("Found method: {0}", mInfo);
    }
}
type Program() =
    // Method to get:
    member _.MethodA() = ()

// Get MethodA()
let mInfo = typeof<Program>.GetMethod "MethodA"
printfn $"Found method: {mInfo}"

Imports System.Reflection

Class Program

    ' Method to get:
    Public Sub MethodA()
    End Sub


    Public Shared Sub Main(ByVal args() As String)

        ' Get MethodA()
        Dim mInfo As MethodInfo = GetType(Program).GetMethod("MethodA")
        Console.WriteLine("Found method: {0}", mInfo)

    End Sub
End Class

備註

name搜尋會區分大小寫。 搜尋包含公用靜態和公用實例方法。

如果方法已多載且有多個公用方法,則 GetMethod(String) 方法會 AmbiguousMatchException 擲回例外狀況。 在下列範例中,會擲回例外狀況,因為方法有多個公用多載 Int32.ToString 。 另一方面,因為 Person.ToString 方法會 Object.ToString 覆寫,因此不會多載, GetMethod(String) 所以方法可以擷取 MethodInfo 物件。

using System;
using System.Reflection;

public class Person
{
    public String FirstName;
    public String LastName;

    public override String ToString()
    {
        return (FirstName + " " + LastName).Trim();
    }
}

public class Example2
{
    public static void Main()
    {
        Type t = typeof(Person);
        RetrieveMethod(t, "ToString");

        t = typeof(Int32);
        RetrieveMethod(t, "ToString");
    }

    private static void RetrieveMethod(Type t, String name)
    {
        try
        {
            MethodInfo m = t.GetMethod(name);
            if (m != null)
                Console.WriteLine("{0}.{1}: {2} method", m.ReflectedType.Name,
                                  m.Name, m.IsStatic ? "Static" : "Instance");
            else
                Console.WriteLine("{0}.ToString method not found", t.Name);
        }
        catch (AmbiguousMatchException)
        {
            Console.WriteLine("{0}.{1} has multiple public overloads.",
                              t.Name, name);
        }
    }
}
// The example displays the following output:
//       Person.ToString: Instance method
//       Int32.ToString has multiple public overloads.
open System
open System.Reflection

type Person() =
    member val FirstName = "" with get, set
    member val LastName = "" with get, set

    override this.ToString() =
        (this.FirstName + " " + this.LastName).Trim()

let retrieveMethod (t: Type) name =
    try
        let m = t.GetMethod name
        if m <> null then
            printfn $"""{m.ReflectedType.Name}.{m.Name}: {if m.IsStatic then "Static" else "Instance"} method"""
        else
            printfn $"{t.Name}.ToString method not found"
    with :? AmbiguousMatchException ->
        printfn $"{t.Name}.{name} has multiple public overloads."

let t = typeof<Person>
retrieveMethod t "ToString"

let t2 = typeof<int>
retrieveMethod t2 "ToString"

// The example displays the following output:
//       Person.ToString: Instance method
//       Int32.ToString has multiple public overloads.
Imports System.Reflection

Public Class Person
   Public FirstName As String
   Public LastName As String
   
   Public Overrides Function ToString() As String
      Return (FirstName + " " + LastName).Trim()
   End Function
End Class

Module Example
   Public Sub Main()
      Dim t As Type = GetType(Person)
      RetrieveMethod(t, "ToString")
      
      t = GetType(Int32)
      RetrieveMethod(t, "ToString")
   End Sub
   
   Private Sub RetrieveMethod(t As Type, name As String)   
      Try
         Dim m As MethodInfo = t.GetMethod(name)
         If m IsNot Nothing Then
            Console.WriteLine("{0}.{1}: {2} method", m.ReflectedType.Name,
                              m.Name, If(m.IsStatic, "Static", "Instance"))    
         Else
            Console.WriteLine("{0}.ToString method not found", t.Name)
         End If   
      Catch e As AmbiguousMatchException
         Console.WriteLine("{0}.{1} has multiple public overloads.", 
                           t.Name, name)
      End Try
   End Sub
End Module
' The example displays the following output:
'       Person.ToString: Instance method
'       Int32.ToString has multiple public overloads.

您可以執行下列其中一項來擷取特定方法:

  • GetMethod(String, BindingFlags)呼叫 方法,並指定可唯一bindingAttr識別方法的自變數。 例如,如果因為類型具有靜態和實例多載而擲回例外狀況,您可以指定 bindingAttrBindingFlags.InstanceOrBindingFlags.Instance自變數。

  • 呼叫 方法的多 GetMethod 載,其中包含 types 定義方法參數類型的參數。

  • GetMethods()呼叫 方法,以擷取陣列,其中包含屬於型別的所有公用方法。 然後,您可以逐一查看,以識別名為 的 name重複方法。

如果目前 Type 代表建構的泛型型別,這個方法會傳回 MethodInfo ,其類型參數會由適當的型別自變數取代。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的方法,如果沒有類別條件約束,則為的 Object 方法。

注意

針對泛型方法,請勿在 中包含 name型別自變數。 例如,C# 程式代碼 GetMember("MyMethod<int>") 會搜尋具有文字名稱 「MyMethod<int>的成員,而不是名為 的方法 MyMethod ,其類型為的一個泛型 int自變數。

另請參閱

適用於

GetMethod(String, Type[], ParameterModifier[])

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

搜尋指定的公用方法,其參數符合指定的引數類型和修飾詞。

public:
 System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public:
 virtual System::Reflection::MethodInfo ^ GetMethod(System::String ^ name, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.MethodInfo? GetMethod (string name, Type[] types, System.Reflection.ParameterModifier[]? modifiers);
public System.Reflection.MethodInfo GetMethod (string name, Type[] types, System.Reflection.ParameterModifier[] modifiers);
member this.GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
abstract member GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
override this.GetMethod : string * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.MethodInfo
Public Function GetMethod (name As String, types As Type(), modifiers As ParameterModifier()) As MethodInfo

參數

name
String

字串,包含要取得的公用方法名稱。

types
Type[]

Type 物件的陣列,代表所要取得之方法的參數數目、順序和類型。

-或-

Type 物件的空陣列 (由 EmptyTypes 欄位提供),可取得不採用參數的方法。

modifiers
ParameterModifier[]

ParameterModifier 物件的陣列,代表在 types 陣列中與對應項目關聯的屬性。 只用於透過 COM Interop 進行呼叫以及只處理以傳址方式傳遞的參數。 預設的繫結器不會處理這個參數。

傳回

物件,代表符合指定之需求的公用方法 (如有找到),否則為 null

實作

例外狀況

找到一個以上具有指定名稱及指定參數的方法。

namenull

-或-

typesnull

-或-

types 的其中一個項目為 null

types 是多維的。

-或-

modifiers 是多維的。

備註

雖然預設系結器不會處理 ParameterModifier 參數 (modifiers) ,但您可以使用抽象 System.Reflection.Binder 類來撰寫會處理 modifiers的自定義系結器。 ParameterModifier 只有在透過 COM Interop 呼叫時,才會使用,而且只會處理以傳址方式傳遞的參數。

name搜尋會區分大小寫。 搜尋包含公用靜態和公用實例方法。

注意

查閱建構函式和方法時,您無法省略參數。 您只能在叫用時省略參數。

如果目前 Type 代表建構的泛型型別,這個方法會傳回 MethodInfo ,其類型參數會由適當的型別自變數取代。

如果目前的 Type 代表泛型型別或泛型方法定義中的型別參數,這個方法會搜尋類別條件約束的方法,如果沒有類別條件約束,則為的 Object 方法。

注意

針對泛型方法,請勿在 中包含 name型別自變數。 例如,C# 程式代碼 GetMethod("MyMethod<int>") 會搜尋具有文字名稱 「MyMethod<int>的成員,而不是名為 的方法 MyMethod ,其類型為的一個泛型 int自變數。 請改用 GetMethod("MyMethod") 陣列中的 types 適當參數。

另請參閱

適用於