英語で読む

次の方法で共有


Type.DeclaringMethod プロパティ

定義

現在の MethodBase がジェネリック メソッドの型パラメーターを表している場合に、宣言するメソッドを表す Type を取得します。

C#
public virtual System.Reflection.MethodBase? DeclaringMethod { get; }
C#
public virtual System.Reflection.MethodBase DeclaringMethod { get; }

プロパティ値

現在の Type がジェネリック メソッドの型パラメーターを表している場合は、宣言メソッドを表す MethodBase。それ以外の場合は null

次のコード例では、ジェネリック メソッドを持つクラスを定義し、メソッドに型引数を割り当て、結果として構築されたジェネリック メソッドを呼び出します。 また、ジェネリック メソッド定義と構築されたメソッドに関する情報も表示されます。 ジェネリック メソッド定義の型パラメーターに関する情報を表示する場合、 メソッドのコード例では DisplayGenericMethodInfo 、メソッドのジェネリック型パラメーターの プロパティの DeclaringMethod 値を示します。

C#
using System;
using System.Reflection;

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

public class Test
{
    public static void Main()
    {
        Console.WriteLine("\r\n--- Examine a generic method.");

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

        // Invoke the method.
        object[] args = {42};
        miConstructed.Invoke(null, args);

        // Invoke the method normally.
        Example.Generic<int>(42);

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

    private static void DisplayGenericMethodInfo(MethodInfo mi)
    {
        Console.WriteLine("\r\n{0}", mi);

        Console.WriteLine("\tIs this a generic method definition? {0}",
            mi.IsGenericMethodDefinition);

        Console.WriteLine("\tIs it a generic method? {0}",
            mi.IsGenericMethod);

        Console.WriteLine("\tDoes it have unassigned generic parameters? {0}",
            mi.ContainsGenericParameters);

        // If this is a generic method, display its type arguments.
        //
        if (mi.IsGenericMethod)
        {
            Type[] typeArguments = mi.GetGenericArguments();

            Console.WriteLine("\tList type arguments ({0}):",
                typeArguments.Length);

            foreach (Type tParam in typeArguments)
            {
                // IsGenericParameter is true only for generic type
                // parameters.
                //
                if (tParam.IsGenericParameter)
                {
                    Console.WriteLine("\t\t{0}  parameter position {1}" +
                        "\n\t\t   declaring method: {2}",
                        tParam,
                        tParam.GenericParameterPosition,
                        tParam.DeclaringMethod);
                }
                else
                {
                    Console.WriteLine("\t\t{0}", tParam);
                }
            }
        }
    }
}

/* This example produces the following output:

--- Examine a generic method.

Void Generic[T](T)
        Is this a generic method definition? True
        Is it a generic method? True
        Does it have unassigned generic parameters? True
        List type arguments (1):
                T  parameter position 0
                   declaring method: Void Generic[T](T)

Void Generic[Int32](Int32)
        Is this a generic method definition? False
        Is it a generic method? True
        Does it have unassigned generic parameters? False
        List type arguments (1):
                System.Int32

Here it is: 42

Here it is: 42

The definition is the same: True

 */

注釈

宣言メソッドはジェネリック メソッド定義です。 つまり、 が を返さない場合DeclaringMethodは をDeclaringMethod.IsGenericMethodDefinition返しますtruenull

プロパティと DeclaringMethod プロパティはDeclaringType、ジェネリック型パラメーターが最初に定義されたジェネリック型定義またはジェネリック メソッド定義を識別します。

  • プロパティが DeclaringMethod ジェネリック メソッド定義を MethodInfo表す MethodInfo を返し、現在 Type の オブジェクトがそのジェネリック メソッド定義の型パラメーターを表す場合。

  • プロパティが を DeclaringMethod 返す null場合、プロパティは DeclaringType 常にジェネリック型定義を表すオブジェクトを返 Type し、現在 Type の オブジェクトはそのジェネリック型定義の型パラメーターを表します。

  • プロパティが を DeclaringMethod スローする IsGenericParameter 型の プロパティ falseInvalidOperationException取得します。

MethodBaseプロパティによってDeclaringMethod返される は、ジェネリック メソッドの場合は MethodInfo であるか、ConstructorInfoジェネリック コンストラクターの場合は です。

注意

.NET Framework バージョン 2.0 では、ジェネリック コンストラクターはサポートされていません。

ジェネリック リフレクションで使用する用語に関する一定の条件の一覧については、IsGenericType プロパティの解説を参照してください。

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

こちらもご覧ください