MethodInfo.GetGenericMethodDefinition Metodo

Definizione

Restituisce un oggetto MethodInfo che rappresenta la definizione di un metodo generica da cui è possibile costruire il metodo corrente.

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

Restituisce

MethodInfo

Oggetto MethodInfo che rappresenta la definizione di un metodo generica da cui è possibile costruire il metodo corrente.

Attributi

Eccezioni

Il metodo corrente non è un metodo generico. Ciò significa che IsGenericMethod restituisce false.

Questo metodo non è supportato.

Esempio

Nell'esempio di codice seguente viene illustrata una classe con un metodo generico e il codice necessario per ottenere un per il metodo , associare il metodo agli argomenti di tipo e recuperare la definizione di tipo generico originale dal metodo MethodInfo associato.

Questo esempio fa parte di un esempio più ampio fornito per il MakeGenericMethod metodo .

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

Commenti

Una definizione di metodo generico è un modello da cui è possibile costruire i metodi. Ad esempio, dalla definizione di metodo generico (espressa nella T M<T>(T t) sintassi C#, in Visual Basic) è possibile costruire e richiamare il metodo Function M(Of T)(ByVal tVal As T) As T ( in int M<int>(int t) Function M(Of Integer)(ByVal tVal As Integer) As Integer Visual Basic). Dato un MethodInfo oggetto che rappresenta questo metodo costruito, il metodo restituisce la definizione del metodo GetGenericMethodDefinition generico.

Se vengono creati due metodi costruiti dalla stessa definizione di metodo generico, il GetGenericMethodDefinition metodo restituisce lo stesso oggetto per entrambi i MethodInfo metodi.

Se si chiama GetGenericMethodDefinition su un oggetto che rappresenta già una definizione di metodo MethodInfo generico, restituisce l'oggetto MethodInfo corrente.

Se una definizione di metodo generico include parametri generici del tipo dichiarante, sarà presente una definizione di metodo generica specifica per ogni tipo costruito. Si consideri ad esempio il codice 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() {...};  
};  

Nel tipo costruito C<int> ( C(Of Integer) in Visual Basic), il metodo generico restituisce M B<int, S> . Nel tipo aperto C<T> restituisce M B<T, S> . In entrambi i casi, IsGenericMethodDefinition la proprietà restituisce per true MethodInfo l'oggetto che rappresenta , quindi può essere chiamato su entrambi gli oggetti M MakeGenericMethod MethodInfo . Nel caso del tipo costruito, il risultato della chiamata MakeGenericMethod è un oggetto che può essere MethodInfo richiamato. Nel caso del tipo aperto, MethodInfo l'oggetto restituito da MakeGenericMethod non può essere richiamato.

Per un elenco delle condizioni invarianti per termini specifici dei metodi generici, vedere la IsGenericMethod proprietà . Per un elenco delle condizioni invarianti per altri termini usati nella reflection generica, vedere la IsGenericType proprietà .

Si applica a

Vedi anche