MethodInfo.GetGenericMethodDefinition MethodInfo.GetGenericMethodDefinition MethodInfo.GetGenericMethodDefinition MethodInfo.GetGenericMethodDefinition Method

定义

返回一个 MethodInfo 对象,该对象表示可从其构造当前方法的泛型方法定义。Returns a MethodInfo object that represents a generic method definition from which the current method can be constructed.

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
Public Overridable Function GetGenericMethodDefinition () As MethodInfo

返回

一个 MethodInfo 对象,表示可从其构造当前方法的泛型方法定义。A MethodInfo object representing a generic method definition from which the current method can be constructed.

异常

当前方法不是泛型方法。The current method is not a generic method. 也就是说,IsGenericMethod 返回 falseThat is, IsGenericMethod returns false.

不支持此方法。This method is not supported.

示例

下面的代码示例演示一个具有泛型方法的类和获取MethodInfo方法的所需的代码,将方法绑定到类型参数,并从绑定方法获取原始泛型类型定义。The following code example shows a class with a generic method and the code required to obtain a MethodInfo for the method, bind the method to type arguments, and get the original generic type definition back from the bound method.

此示例摘自为MakeGenericMethod方法提供的一个更大的示例。This example is part of a larger example provided for the MakeGenericMethod method.

// 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)

注解

泛型方法定义是可以从中构造方法的模板。A generic method definition is a template from which methods can be constructed. 例如,从泛型方法定义T M<T>(T t) (用C#语法表示),在 Visual Basic)中,可以构造和调用方法int M<int>(int t)Function M(Of Integer)(ByVal tVal As Integer) As Integer在 Visual Basic 中)。 Function M(Of T)(ByVal tVal As T) As TFor example, from the generic method definition T M<T>(T t) (expressed in C# syntax; Function M(Of T)(ByVal tVal As T) As T in Visual Basic) you can construct and invoke the method int M<int>(int t) (Function M(Of Integer)(ByVal tVal As Integer) As Integer in Visual Basic). 给定表示此构造方法的GetGenericMethodDefinition 对象,方法返回泛型方法定义。MethodInfoGiven a MethodInfo object representing this constructed method, the GetGenericMethodDefinition method returns the generic method definition.

如果两个构造方法是从同一个泛型方法定义创建的GetGenericMethodDefinition ,则该方法MethodInfo将为这两个方法返回相同的对象。If two constructed methods are created from the same generic method definition, the GetGenericMethodDefinition method returns the same MethodInfo object for both methods.

如果在已GetGenericMethodDefinition表示泛型MethodInfo方法定义的上调用,则它将返回当前MethodInfoIf you call GetGenericMethodDefinition on a MethodInfo that already represents a generic method definition, it returns the current MethodInfo.

如果泛型方法定义包含声明类型的泛型参数,则会有特定于每个构造类型的泛型方法定义。If a generic method definition includes generic parameters of the declaring type, there will be a generic method definition specific to each constructed type. 例如,请考虑以下C#内容,Visual Basic 和C++代码:For example, consider the following C#, Visual Basic, and C++ code:

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() {...};  
};  

在构造类型C<int>C(Of Integer)在 Visual Basic)中,泛型方法M返回B<int, S>In the constructed type C<int> (C(Of Integer) in Visual Basic), the generic method M returns B<int, S>. 在开放类型C<T>中, M返回B<T, S>In the open type C<T>, M returns B<T, S>. 在这两种情况IsGenericMethodDefinition下, true属性将MethodInfo返回表示M的, MakeGenericMethod因此可以在两个MethodInfo对象上调用。In both cases, the IsGenericMethodDefinition property returns true for the MethodInfo that represents M, so MakeGenericMethod can be called on both MethodInfo objects. 对于构造类型,调用MakeGenericMethod的结果MethodInfo是可调用的。In the case of the constructed type, the result of calling MakeGenericMethod is a MethodInfo that can be invoked. 对于开放类型, MethodInfo MakeGenericMethod无法调用返回的。In the case of the open type, the MethodInfo returned by MakeGenericMethod cannot be invoked.

有关特定于泛型方法的术语的固定条件列表,请参见IsGenericMethod属性。For a list of the invariant conditions for terms specific to generic methods, see the IsGenericMethod property. 有关泛型反射中使用的其他术语的固定条件列表,请参见IsGenericType属性。For a list of the invariant conditions for other terms used in generic reflection, see the IsGenericType property.

适用于

另请参阅