MethodInfo.GetGenericMethodDefinition Método

Definição

Retorna um objeto MethodInfo que representa uma definição de método genérico da qual o método atual pode ser criado.

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

Retornos

MethodInfo

Um objeto MethodInfo que representa uma definição de método genérico da qual o método atual pode ser criado.

Atributos

Exceções

O método atual não é um método genérico. Ou seja, IsGenericMethod retorna false.

Não há suporte para o método.

Exemplos

O exemplo de código a seguir mostra uma classe com um método genérico e o código necessário para obter um MethodInfo para o método, associar o método aos argumentos de tipo e obter a definição de tipo genérico original de volta do método associado.

Este exemplo faz parte de um exemplo maior fornecido para o MakeGenericMethod método .

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

Comentários

Uma definição de método genérico é um modelo do qual os métodos podem ser construídos. por exemplo, da definição do método genérico T M<T>(T t) (expresso em sintaxe C#; Function M(Of T)(ByVal tVal As T) As T em Visual Basic), você pode construir e invocar o método int M<int>(int t) ( Function M(Of Integer)(ByVal tVal As Integer) As Integer em Visual Basic). Dado um MethodInfo objeto que representa esse método construído, o GetGenericMethodDefinition método retorna a definição do método genérico.

Se dois métodos construídos forem criados a partir da mesma definição de método genérico, o GetGenericMethodDefinition método retornará o mesmo MethodInfo objeto para ambos os métodos.

Se você chamar GetGenericMethodDefinition em um MethodInfo que já representa uma definição de método genérico, ele retornará o atual MethodInfo .

Se uma definição de método genérico incluir parâmetros genéricos do tipo declarativo, haverá uma definição de método genérico específica para cada tipo construído. por exemplo, considere o seguinte código C#, Visual Basic e 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() {...};  
};  

no tipo construído C<int> ( C(Of Integer) em Visual Basic), o método genérico M retorna B<int, S> . No tipo Open C<T> , M retorna B<T, S> . Em ambos os casos, a IsGenericMethodDefinition propriedade retorna true para o MethodInfo que representa M , portanto, MakeGenericMethod pode ser chamado em ambos os MethodInfo objetos. No caso do tipo construído, o resultado da chamada MakeGenericMethod é um MethodInfo que pode ser invocado. No caso do tipo Open, o MethodInfo retornado por MakeGenericMethod não pode ser invocado.

Para obter uma lista das condições invariáveis para termos específicos para métodos genéricos, consulte a IsGenericMethod propriedade. Para obter uma lista das condições invariáveis para outros termos usados em reflexão genérica, consulte a IsGenericType propriedade.

Aplica-se a

Confira também