MethodInfo.GetGenericMethodDefinition メソッド

定義

現在のメソッドを構築する元になるジェネリック メソッド定義を表す MethodInfo オブジェクトを返します。

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

戻り値

現在のメソッドを構築する元になるジェネリック メソッド定義を表す MethodInfo オブジェクト。

属性

例外

現在のメソッドはジェネリック メソッドではありません。 つまり、IsGenericMethodfalse を返します。

このメソッドはサポートされていません。

次のコード例は、ジェネリック メソッドを持つクラスと、 メソッドの を取得 MethodInfo し、メソッドを型引数にバインドし、バインドされたメソッドから元のジェネリック型定義を取得するために必要なコードを示しています。

この例は、 メソッドに対して提供される大きな例の MakeGenericMethod 一部です。

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

注釈

ジェネリック メソッド定義は、メソッドを構築できるテンプレートです。 たとえば、ジェネリック メソッド定義 T M<T>(T t) (C# 構文で表され、 Function M(Of T)(ByVal tVal As T) As T Visual Basic では ) から、 メソッド int M<int>(int t) (Function M(Of Integer)(ByVal tVal As Integer) As Integer Visual Basic では) を構築して呼び出すことができます。 MethodInfoこの構築されたメソッドを表す オブジェクトを指定すると、 メソッドはGetGenericMethodDefinitionジェネリック メソッド定義を返します。

同じジェネリック メソッド定義から 2 つの構築メソッドが作成された場合、メソッドは GetGenericMethodDefinition 両方のメソッドに対して同じ MethodInfo オブジェクトを返します。

既にジェネリック メソッド定義を表す に対MethodInfoして を呼び出GetGenericMethodDefinitionすと、現在MethodInfoの が返されます。

ジェネリック メソッド定義に宣言型のジェネリック パラメーターが含まれている場合は、構築された各型に固有のジェネリック メソッド定義が存在します。 たとえば、次の C#、Visual Basic、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() {...};
};

構築された型 C<int> (C(Of Integer) Visual Basic の場合) では、ジェネリック メソッド M は を返します B<int, S>。 開いている型 C<T>では、 を M 返します B<T, S>。 どちらの場合も、 プロパティは IsGenericMethodDefinition を表す MMethodInfo を返trueすのでMakeGenericMethod、両方MethodInfoのオブジェクトで呼び出すことができます。 構築された型の場合、呼び出しの結果はMethodInfo呼び出MakeGenericMethodすことができる です。 開いている型の場合、 によってMakeGenericMethod返される をMethodInfo呼び出すことはできません。

ジェネリック メソッドに固有の用語の不変条件の一覧については、 プロパティを IsGenericMethod 参照してください。 ジェネリック リフレクションで使用される他の用語の不変条件の一覧については、 プロパティを IsGenericType 参照してください。

適用対象

こちらもご覧ください