MethodInfo.GetGenericMethodDefinition 方法

定義

傳回表示泛型方法定義的 MethodInfo 物件,利用此泛型方法定義就可以建構出目前的方法。

public:
 virtual System::Reflection::MethodInfo ^ GetGenericMethodDefinition();
public virtual System.Reflection.MethodInfo GetGenericMethodDefinition ();
[System.Runtime.InteropServices.ComVisible(true)]
public virtual System.Reflection.MethodInfo GetGenericMethodDefinition ();
abstract member GetGenericMethodDefinition : unit -> System.Reflection.MethodInfo
override this.GetGenericMethodDefinition : unit -> System.Reflection.MethodInfo
[<System.Runtime.InteropServices.ComVisible(true)>]
abstract member GetGenericMethodDefinition : unit -> System.Reflection.MethodInfo
override this.GetGenericMethodDefinition : unit -> System.Reflection.MethodInfo
Public Overridable Function GetGenericMethodDefinition () As MethodInfo

傳回

傳回代表泛型方法定義的 MethodInfo 物件,利用這個泛型方法定義就可以建構出目前的方法。

屬性

例外狀況

目前的方法不是泛型方法。 亦即,IsGenericMethod 會傳回 false

不支援這個方法。

範例

下列程式碼範例示範具有泛型方法的類別,以及取得 MethodInfo 方法所需的程式碼、將 方法系結至型別引數,以及從系結方法取得原始泛型型別定義。

這個範例是針對 方法提供之較大範例的 MakeGenericMethod 一部分。

// Define a class with a generic method.
ref class Example
{
public:
    generic<typename T> static void Generic(T toDisplay)
    {
        Console::WriteLine("\r\nHere it is: {0}", toDisplay);
    }
};
// Define a class with a generic method.
public class Example
{
    public static void Generic<T>(T toDisplay)
    {
        Console.WriteLine("\r\nHere it is: {0}", toDisplay);
    }
}
' Define a class with a generic method.
Public Class Example
    Public Shared Sub Generic(Of T)(ByVal toDisplay As T)
        Console.WriteLine(vbCrLf & "Here it is: {0}", toDisplay)
    End Sub
End Class
// Create a Type object representing class Example, and
// get a MethodInfo representing the generic method.
//
Type^ ex = Example::typeid;
MethodInfo^ mi = ex->GetMethod("Generic");

DisplayGenericMethodInfo(mi);

// Assign the int type to the type parameter of the Example 
// method.
//
MethodInfo^ miConstructed = mi->MakeGenericMethod(int::typeid);

DisplayGenericMethodInfo(miConstructed);
// Create a Type object representing class Example, and
// get a MethodInfo representing the generic method.
//
Type ex = typeof(Example);
MethodInfo mi = ex.GetMethod("Generic");

DisplayGenericMethodInfo(mi);

// Assign the int type to the type parameter of the Example
// method.
//
MethodInfo miConstructed = mi.MakeGenericMethod(typeof(int));

DisplayGenericMethodInfo(miConstructed);
' Create a Type object representing class Example, and
' get a MethodInfo representing the generic method.
'
Dim ex As Type = GetType(Example)
Dim mi As MethodInfo = ex.GetMethod("Generic")

DisplayGenericMethodInfo(mi)

' Assign the Integer type to the type parameter of the Example 
' method.
'
Dim arguments() As Type = { GetType(Integer) }
Dim miConstructed As MethodInfo = mi.MakeGenericMethod(arguments)

DisplayGenericMethodInfo(miConstructed)
// Get the generic type definition from the closed method,
// and show it's the same as the original definition.
//
MethodInfo^ miDef = miConstructed->GetGenericMethodDefinition();
Console::WriteLine("\r\nThe definition is the same: {0}",
        miDef == mi);
// Get the generic type definition from the closed method,
// and show it's the same as the original definition.
//
MethodInfo miDef = miConstructed.GetGenericMethodDefinition();
Console.WriteLine("\r\nThe definition is the same: {0}",
    miDef == mi);
' Get the generic type definition from the constructed method,
' and show that it's the same as the original definition.
'
Dim miDef As MethodInfo = miConstructed.GetGenericMethodDefinition()
Console.WriteLine(vbCrLf & "The definition is the same: {0}", _
    miDef Is mi)

備註

泛型方法定義是可從中建構方法的範本。 例如,從以 C# 語法表示的泛型方法定義 T M<T>(T t) (; Function M(Of T)(ByVal tVal As T) As T 在 Visual Basic) ,您可以在 Visual Basic) 中建構及叫用方法 int M<int>(int t) (Function M(Of Integer)(ByVal tVal As Integer) As Integer 。 假設物件 MethodInfo 代表這個建構的方法,此方法會 GetGenericMethodDefinition 傳回泛型方法定義。

如果從相同的泛型方法定義建立兩個建構方法,則 GetGenericMethodDefinition 方法會針對這兩種方法傳回相同的 MethodInfo 物件。

如果您在已經代表泛型方法定義的 上 MethodInfo 呼叫 GetGenericMethodDefinition ,它會傳回目前的 MethodInfo

如果泛型方法定義包含宣告類型的泛型參數,則每個建構型別都有特定的泛型方法定義。 例如,請考慮下列 C#、Visual Basic 和 C++ 程式碼:

class B<U,V> {}
class C<T> { public B<T,S> M<S>() {...}}

Class B(Of U, V)
End Class
Class C(Of T)
    Public Function M(Of S)() As B(Of T, S)
        ...
    End Function
End Class

generic <typename U, typename V> ref class B {};
generic <typename T> ref class C
{
public:
    generic <typename S> B<T,S>^ M() {...};
};

在 Visual Basic) 的建構類型 C<int> (C(Of Integer) 中,泛型方法 M 會傳 B<int, S> 回 。 在開啟的類型 C<T> 中, MB<T, S> 回 。 在這兩種情況下, IsGenericMethodDefinition 屬性會傳 MethodInfotrue 代表 M 的 ,因此 MakeGenericMethod 可以在這兩個 MethodInfo 物件上呼叫。 在建構型別的情況下,呼叫 MakeGenericMethod 的結果是 MethodInfo 可以叫用的 。 在開啟類型的情況下, MethodInfo 無法叫用 所傳回的 MakeGenericMethod

如需泛型方法特定詞彙的不變異條件清單,請參閱 IsGenericMethod 屬性。 如需泛型反映中所使用之其他詞彙的不變異條件清單,請參閱 IsGenericType 屬性。

適用於

另請參閱