MethodInfo.GetGenericMethodDefinition Metoda

Definicja

Zwraca obiekt reprezentujący definicję MethodInfo metody ogólnej, z której można utworzyć bieżącą metodę.

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

Zwraca

MethodInfo Obiekt reprezentujący definicję metody ogólnej, z której można utworzyć bieżącą metodę.

Atrybuty

Wyjątki

Bieżąca metoda nie jest metodą ogólną. Oznacza to, IsGenericMethod że zwraca wartość false.

Ta metoda nie jest obsługiwana.

Przykłady

Poniższy przykład kodu przedstawia klasę z metodą ogólną i kod wymagany do uzyskania MethodInfo dla metody, powiązanie metody z argumentami typu i pobranie oryginalnej definicji typu ogólnego z powrotem z metody powiązanej.

Ten przykład jest częścią większego przykładu udostępnionego MakeGenericMethod dla metody .

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

Uwagi

Definicja metody ogólnej to szablon, z którego można tworzyć metody. Na przykład z definicji T M<T>(T t) metody ogólnej (wyrażonej w składni języka C#; Function M(Of T)(ByVal tVal As T) As T w Visual Basic) można skonstruować i wywołać metodę int M<int>(int t) (Function M(Of Integer)(ByVal tVal As Integer) As Integer w Visual Basic). Biorąc pod uwagę obiekt reprezentujący tę skonstruowaną MethodInfo metodę, GetGenericMethodDefinition metoda zwraca definicję metody ogólnej.

Jeśli dwie skonstruowane metody są tworzone na podstawie tej samej definicji metody ogólnej, GetGenericMethodDefinition metoda zwraca ten sam MethodInfo obiekt dla obu metod.

Jeśli wywołasz GetGenericMethodDefinition metodę MethodInfo , która reprezentuje już definicję metody ogólnej, zwraca bieżący MethodInfoelement .

Jeśli definicja metody ogólnej zawiera ogólne parametry typu deklarowanego, będzie istnieć definicja metody ogólnej specyficzna dla każdego skonstruowanego typu. Rozważmy na przykład następujący kod C#, Visual Basic i 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() {...};
};

W skonstruowanym typie C<int> (C(Of Integer) w Visual Basic) metoda ogólna zwraca wartość B<int, S>M . W typie C<T>M open zwraca wartość B<T, S>. W obu przypadkach IsGenericMethodDefinition właściwość zwraca true wartość , MethodInfo która reprezentuje Melement , więc MakeGenericMethod można wywołać dla obu MethodInfo obiektów. W przypadku skonstruowanego typu wynik wywołania MakeGenericMethod jest elementem MethodInfo , który można wywołać. W przypadku typu MethodInfo otwartego nie można wywołać zwracanego przez MakeGenericMethod program .

Aby uzyskać listę niezmiennych warunków dotyczących warunków specyficznych dla metod ogólnych, zobacz IsGenericMethod właściwość . Aby uzyskać listę niezmiennych warunków dla innych terminów używanych w odbiciu ogólnym, zobacz IsGenericType właściwość .

Dotyczy

Zobacz też