MethodInfo.GetGenericMethodDefinition Método

Definición

Devuelve un objeto MethodInfo que representa una definición de método genérico a partir de la cual se puede construir el método genérico actual.

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

Devoluciones

MethodInfo

Objeto MethodInfo que representa una definición de método genérico a partir de la cual se puede construir el método actual.

Atributos

Excepciones

El método actual no es genérico. Es decir, IsGenericMethod devuelve false.

No se admite este método.

Ejemplos

En el ejemplo de código siguiente se muestra una clase con un método genérico y el código necesario para obtener para MethodInfo el método , enlazar el método a argumentos de tipo y obtener la definición de tipo genérica original del método enlazado.

Este ejemplo forma parte de un ejemplo más grande proporcionado para el 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)

Comentarios

Una definición de método genérico es una plantilla a partir de la cual se pueden construir métodos. Por ejemplo, desde la definición T M<T>(T t) de método genérico (expresada en la sintaxis de C#; Function M(Of T)(ByVal tVal As T) As T en Visual Basic), puede construir e invocar el método int M<int>(int t) (Function M(Of Integer)(ByVal tVal As Integer) As Integer en Visual Basic). Dado un MethodInfo objeto que representa este método construido, el GetGenericMethodDefinition método devuelve la definición del método genérico.

Si se crean dos métodos construidos a partir de la misma definición de método genérico, el GetGenericMethodDefinition método devuelve el mismo MethodInfo objeto para ambos métodos.

Si llama a GetGenericMethodDefinition en un MethodInfo objeto que ya representa una definición de método genérico, devuelve el objeto actual MethodInfo.

Si una definición de método genérico incluye parámetros genéricos del tipo declarante, habrá una definición de método genérica específica de cada tipo construido. Por ejemplo, considere el siguiente código de C#, Visual Basic y 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() {...};  
};  

En el tipo C<int> construido (C(Of Integer) en Visual Basic), el método M genérico devuelve B<int, S>. En el tipo C<T>abierto , M devuelve B<T, S>. En ambos casos, la IsGenericMethodDefinition propiedad devuelve true para que MethodInfo represente M, por lo que MakeGenericMethod se puede llamar a en ambos MethodInfo objetos. En el caso del tipo construido, el resultado de llamar MakeGenericMethod a es un MethodInfo objeto que se puede invocar. En el caso del tipo abierto, no se puede invocar el MethodInfo devuelto por MakeGenericMethod .

Para obtener una lista de las condiciones invariables para los términos específicos de los métodos genéricos, vea la IsGenericMethod propiedad . Para obtener una lista de las condiciones invariables para otros términos usados en la reflexión genérica, vea la IsGenericType propiedad .

Se aplica a

Consulte también