英語で読む

次の方法で共有


Module.ResolveMethod メソッド

定義

メタデータ トークンで識別されるメソッドを返します。

オーバーロード

ResolveMethod(Int32, Type[], Type[])

指定したジェネリック型パラメーターで定義されたコンテキストの、指定したメタデータ トークンで識別されるメソッドまたはコンストラクターを返します。

ResolveMethod(Int32)

メソッドまたは指定したメタデータ トークンによって識別されるコンス トラクターを返します。

ResolveMethod(Int32, Type[], Type[])

ソース:
Module.cs
ソース:
Module.cs
ソース:
Module.cs

指定したジェネリック型パラメーターで定義されたコンテキストの、指定したメタデータ トークンで識別されるメソッドまたはコンストラクターを返します。

C#
public virtual System.Reflection.MethodBase? ResolveMethod (int metadataToken, Type[]? genericTypeArguments, Type[]? genericMethodArguments);
C#
public virtual System.Reflection.MethodBase ResolveMethod (int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
C#
public System.Reflection.MethodBase ResolveMethod (int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);

パラメーター

metadataToken
Int32

メソッドまたはモジュール内のコンス トラクターを識別するメタデータ トークン。

genericTypeArguments
Type[]

トークンがスコープ内にある型のジェネリック型引数を表す Type オブジェクトの配列。その型がジェネリックではない場合は null

genericMethodArguments
Type[]

トークンがスコープ内にあるメソッドのジェネリック型引数を表す Type オブジェクトの配列。そのメソッドがジェネリックではない場合は null

戻り値

指定したメタデータ トークンで識別されるメソッドを表す MethodBase オブジェクト。

例外

metadataToken は、現在のモジュールのスコープに含まれるメソッドまたはコンストラクターのトークンではありません。

- または -

metadataToken は、要素型 var (ジェネリック型の型パラメーター) または mvar (ジェネリック メソッドの型パラメーター) を含むシグネチャを持つ MethodSpec ですが、必要なジェネリック型の引数が genericTypeArgumentsgenericMethodArguments の両方またはいずれかに提供されていません。

metadataToken は、現在のモジュールのスコープで有効なトークンではありません。

次の例では、 メソッドの ResolveMethod 2 つのオーバーロードを使用して、ジェネリック コンテキストと非ジェネリック コンテキストの呼び出しサイトからメタデータ トークンを解決する方法を示します。

このコード例では、 G1<Tg1> ジェネリック メソッドを持つ 2 つのジェネリック型 と G2<Tg2>を定義しています。 G1<Tg1> には、そのパラメーターに型パラメーター Tg1 を使用する非ジェネリック メソッドもあります。 型G2<Tg2>のジェネリック メソッドGM2<Tgm2>には、いくつかのメソッド呼び出しが含まれています。

  • ケース 1: ジェネリック メソッドGM1<Tgm1>は、 および GM2<Tgm2> の型パラメーターG2<Tg2>を型引数として使用して呼び出されます。 言い換えると、呼び出されるメソッドのパラメーター型は、 の型定義 G2<Tg2>から閉じたジェネリック型を構築するために使用される型によって異なります。

  • ケース 2: 非ジェネリック メソッド M1 が呼び出されます。 このメソッドの パラメーターでは、定義する型 の型パラメーター を使用します。この場合は、 G1<Tg1>外側の型 G2<Tg2>の型パラメーター である に置き換えられます。

  • ケース 3: ジェネリック メソッドGM1<Tgm1>が呼び出され、ジェネリック型とObjectジェネリック メソッドの型引数に それぞれ と が指定Int32されます。 このメソッド呼び出しは、外側の型またはメソッドの型パラメーターに依存しません。

  • ケース 4: クラスの非ジェネリック メソッド M1Example 呼び出されます。 このメソッド呼び出しは、外側の型またはメソッドの型パラメーターに依存しません。

さらに、この例では非ジェネリック Example クラスを定義します。 このクラスには、ジェネリック メソッド M の呼び出しを行うメソッドがあります。

  • ケース 5: ジェネリック メソッドGM1<Tgm1>が呼び出され、ジェネリック型とObjectジェネリック メソッドの型引数に それぞれ と が指定Int32されます。 このメソッドのコンテキストには、ジェネリック型またはジェネリック メソッドを囲むがありません。

各ケースについて、この例では最初に呼び出されたメソッドを表す をMethodInfo構築し、 メソッドオーバーロードを使用して ResolveMethod(Int32, Type[], Type[]) トークンを解決します。次にType.GetGenericArguments、 メソッドと MethodInfo.GetGenericArguments メソッドを使用して および genericMethodArguments パラメーターの値をgenericTypeArguments取得します。 メソッドは非ジェネリック コンテキストに対して を返 Type.EmptyTypes すので、この手法はすべてのケースで機能します。 この例では、解決された を構築された MethodInfoMethodInfoと比較します。

次に、メソッド オーバーロードを ResolveMethod(Int32) 使用してトークンを解決します。 これは、メソッド呼び出しがジェネリック コンテキストに依存しないため、ケース 3、4、5 で機能します。 ケース 1 と 2 では、トークンを解決するための情報が不足しているため、例外がスローされます。

メタデータ トークンの値は、列挙型としてハードコーディングされます。 このコード例を変更すると、トークン値が変更される可能性があります。 新しいトークン値を確認するには、コードをコンパイルし、 /TOKENS オプションと共に Ildasm.exe を使用してアセンブリを調べます。 トークンは、呼び出しの時点で確認できます。 列挙に新しい値を挿入し、例を再コンパイルします。

C#
using System;
using System.Reflection;

namespace ResolveMethodExample
{
    // Metadata tokens for the MethodRefs that are to be resolved.
    // If you change this program, some or all of these metadata tokens might
    // change. The new token values can be discovered by compiling the example
    // and examining the assembly with Ildasm.exe, using the /TOKENS option.
    // Recompile the program after correcting the token values.
    enum Tokens
    {
        Case1 = 0x2b000001,
        Case2 = 0x0A000006,
        Case3 = 0x2b000002,
        Case4 = 0x06000006,
        Case5 = 0x2b000002
    }

    class G1<Tg1>
    {
        public void GM1<Tgm1> (Tg1 param1, Tgm1 param2) {}
        public void M1(Tg1 param) {}
    }

    class G2<Tg2>
    {
        public void GM2<Tgm2> (Tg2 param1, Tgm2 param2)
        {
            // Case 1: A generic method call that depends on its generic
            // context, because it uses the type parameters of the enclosing
            // generic type G2 and the enclosing generic method GM2. The token
            // for the MethodSpec is Tokens.Case1.
            G1<Tg2> g = new G1<Tg2>();
            g.GM1<Tgm2>(param1, param2);

            // Case 2: A non-generic method call that depends on its generic
            // context, because it uses the type parameter of the enclosing
            // generic type G2. The token for the MemberRef is Tokens.Case2.
            g.M1(param1);

            // Case 3: A generic method call that does not depend on its generic
            // context, because it does not use type parameters of the enclosing
            // generic type or method. The token for the MethodSpec is Tokens.Case3.
            G1<int> gi = new G1<int>();
            gi.GM1<object>(42, new Object());

            // Case 4: A non-generic method call that does not depend on its
            // generic context, because it does not use the type parameters of the
            // enclosing generic type or method. The token for the MethodDef is
            // Tokens.Case4.
            Example e = new Example();
            e.M();
        }
    }

    class Example
    {
        public void M()
        {
            G1<int> g = new G1<int>();
            // Case 5: A generic method call that does not have any generic
            // context. The token for the MethodSpec is Tokens.Case5.
            g.GM1<object>(42, new Object());
        }

        static void Main ()
        {
            Module mod = typeof(Example).Assembly.ManifestModule;
            MethodInfo miResolved2 = null;

            // Case 1: A generic method call that is dependent on its generic context.
            //
            // Create and display a MethodInfo representing the MethodSpec of the
            // generic method g.GM1<Tgm2>() that is called in G2<Tg2>.GM2<Tgm2>().
            Type t = typeof(G1<>).MakeGenericType(typeof(G2<>).GetGenericArguments());
            MethodInfo mi = typeof(G2<>).GetMethod("GM2");
            MethodInfo miTest = t.GetMethod("GM1").MakeGenericMethod(mi.GetGenericArguments());
            Console.WriteLine("\nCase 1:\n{0}", miTest);

            // Resolve the MethodSpec token for method G1<Tg2>.GM1<Tgm2>(), which
            // is called in method G2<Tg2>.GM2<Tgm2>(). The GetGenericArguments method
            // must be used to obtain the context for resolving the method.
            MethodInfo miResolved = (MethodInfo) mod.ResolveMethod(
                (int)Tokens.Case1,
                typeof(G2<>).GetGenericArguments(),
                typeof(G2<>).GetMethod("GM2").GetGenericArguments());
            Console.WriteLine(miResolved);
            Console.WriteLine("Is the resolved method the same? {0}", miResolved == miTest);

            // The overload that doesn't specify generic context throws an exception
            // because there is insufficient context to resolve the token.
            try
            {
                miResolved2 = (MethodInfo) mod.ResolveMethod((int)Tokens.Case1);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}: {1}", ex.GetType(), ex.Message);
            }

            // Case 2: A non-generic method call that is dependent on its generic context.
            //
            // Create and display a MethodInfo representing the MemberRef of the
            // non-generic method g.M1() that is called in G2<Tg2>.GM2<Tgm2>().
            t = typeof(G1<>).MakeGenericType(typeof(G2<>).GetGenericArguments());
            miTest = t.GetMethod("M1");
            Console.WriteLine("\nCase 2:\n{0}", miTest);

            // Resolve the MemberRef token for method G1<Tg2>.M1(), which is
            // called in method G2<Tg2>.GM2<Tgm2>(). The GetGenericArguments method
            // must be used to obtain the context for resolving the method, because
            // the method parameter comes from the generic type G1, and the type
            // argument, Tg2, comes from the generic type that encloses the call.
            // There is no enclosing generic method, so the value Type.EmptyTypes
            // could be passed for the genericMethodArguments parameter.
            miResolved = (MethodInfo) mod.ResolveMethod(
                (int)Tokens.Case2,
                typeof(G2<>).GetGenericArguments(),
                typeof(G2<>).GetMethod("GM2").GetGenericArguments());
            Console.WriteLine(miResolved);
            Console.WriteLine("Is the resolved method the same? {0}", miResolved == miTest);

            // The overload that doesn't specify generic context throws an exception
            // because there is insufficient context to resolve the token.
            try
            {
                miResolved2 = (MethodInfo) mod.ResolveMethod((int)Tokens.Case2);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}: {1}", ex.GetType(), ex.Message);
            }

            // Case 3: A generic method call that is independent of its generic context.
            //
            // Create and display a MethodInfo representing the MethodSpec of the
            // generic method gi.GM1<object>() that is called in G2<Tg2>.GM2<Tgm2>().
            mi = typeof(G1<int>).GetMethod("GM1");
            miTest = mi.MakeGenericMethod(new Type[] { typeof(object) });
            Console.WriteLine("\nCase 3:\n{0}", miTest);

            // Resolve the token for method G1<int>.GM1<object>(), which is called
            // in G2<Tg2>.GM2<Tgm2>(). The GetGenericArguments method is used to
            // obtain the context for resolving the method, but the method call in
            // this case does not use type parameters of the enclosing type or
            // method, so Type.EmptyTypes could be used for both arguments.
            miResolved = (MethodInfo) mod.ResolveMethod(
                (int)Tokens.Case3,
                typeof(G2<>).GetGenericArguments(),
                typeof(G2<>).GetMethod("GM2").GetGenericArguments());
            Console.WriteLine(miResolved);
            Console.WriteLine("Is the resolved method the same? {0}", miResolved == miTest);

            // The method call in this case does not depend on the enclosing generic
            // context, so the token can also be resolved by the simpler overload.
            miResolved2 = (MethodInfo) mod.ResolveMethod((int)Tokens.Case3);

            // Case 4: A non-generic method call that is independent of its generic context.
            //
            // Create and display a MethodInfo representing the MethodDef of the
            // method e.M() that is called in G2<Tg2>.GM2<Tgm2>().
            miTest = typeof(Example).GetMethod("M");
            Console.WriteLine("\nCase 4:\n{0}", miTest);

            // Resolve the token for method Example.M(), which is called in
            // G2<Tg2>.GM2<Tgm2>(). The GetGenericArguments method is used to
            // obtain the context for resolving the method, but the non-generic
            // method call does not use type parameters of the enclosing type or
            // method, so Type.EmptyTypes could be used for both arguments.
            miResolved = (MethodInfo) mod.ResolveMethod(
                (int)Tokens.Case4,
                typeof(G2<>).GetGenericArguments(),
                typeof(G2<>).GetMethod("GM2").GetGenericArguments());
            Console.WriteLine(miResolved);
            Console.WriteLine("Is the resolved method the same? {0}", miResolved == miTest);

            // The method call in this case does not depend on any enclosing generic
            // context, so the token can also be resolved by the simpler overload.
            miResolved2 = (MethodInfo) mod.ResolveMethod((int)Tokens.Case4);

            // Case 5: Generic method call in a non-generic context.
            //
            // Create and display a MethodInfo representing the MethodRef of the
            // closed generic method g.GM1<object>() that is called in Example.M().
            mi = typeof(G1<int>).GetMethod("GM1");
            miTest = mi.MakeGenericMethod(new Type[] { typeof(object) });
            Console.WriteLine("\nCase 5:\n{0}", miTest);

            // Resolve the token for method G1<int>.GM1<object>(), which is called
            // in method Example.M(). The GetGenericArguments method is used to
            // obtain the context for resolving the method, but the enclosing type
            // and method are not generic, so Type.EmptyTypes could be used for
            // both arguments.
            miResolved = (MethodInfo) mod.ResolveMethod(
                (int)Tokens.Case5,
                typeof(Example).GetGenericArguments(),
                typeof(Example).GetMethod("M").GetGenericArguments());
            Console.WriteLine(miResolved);
            Console.WriteLine("Is the resolved method the same? {0}", miResolved == miTest);

            // The method call in this case does not depend on any enclosing generic
            // context, so the token can also be resolved by the simpler overload.
            miResolved2 = (MethodInfo) mod.ResolveMethod((int)Tokens.Case5);
        }
    }
}
/* This example produces the following output:

Case 1:
Void GM1[Tgm2](Tg2, Tgm2)
Void GM1[Tgm2](Tg2, Tgm2)
Is the resolved method the same? True
System.ArgumentException: A BadImageFormatException has been thrown while parsing the signature. This is likely due to lack of a generic context. Ensure genericTypeArguments and genericMethodArguments are provided and contain enough context.

Case 2:
Void M1(Tg2)
Void M1(Tg2)
Is the resolved method the same? True
System.ArgumentException: A BadImageFormatException has been thrown while parsing the signature. This is likely due to lack of a generic context. Ensure genericTypeArguments and genericMethodArguments are provided and contain enough context.

Case 3:
Void GM1[Object](Int32, System.Object)
Void GM1[Object](Int32, System.Object)
Is the resolved method the same? True

Case 4:
Void M()
Void M()
Is the resolved method the same? True

Case 5:
Void GM1[Object](Int32, System.Object)
Void GM1[Object](Int32, System.Object)
Is the resolved method the same? True
 */

注釈

Type.GetGenericArguments ジェネリック型 metadataToken 引数の配列を取得するには、 がスコープ内にある 型で メソッドを使用します genericTypeArguments。 の MethodInfo.GetGenericArguments ジェネリック型引数の配列を取得するには、 がスコープ内にある メソッドで メソッド metadataToken を使用します genericMethodArguments。 これらの引数は、必要ない場合でも、常に安全に指定できます。

注意

メタデータ トークンに関する情報は、共通言語インフラストラクチャ (CLI) のドキュメント(特にパーティション II: メタデータ定義とセマンティクス)にあります。 詳細については、「 ECMA 335 共通言語インフラストラクチャ (CLI)」を参照してください。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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

ResolveMethod(Int32)

ソース:
Module.cs
ソース:
Module.cs
ソース:
Module.cs

メソッドまたは指定したメタデータ トークンによって識別されるコンス トラクターを返します。

C#
public System.Reflection.MethodBase? ResolveMethod (int metadataToken);
C#
public System.Reflection.MethodBase ResolveMethod (int metadataToken);

パラメーター

metadataToken
Int32

メソッドまたはモジュール内のコンス トラクターを識別するメタデータ トークン。

戻り値

指定したメタデータ トークンで識別されるメソッドまたはコンストラクターを表す MethodBase オブジェクト。

例外

metadataToken は、現在のモジュールのスコープに含まれるメソッドまたはコンストラクターのトークンではありません。

- または -

metadataToken は、要素型 var (ジェネリック型の型パラメーター) または mvar (ジェネリック メソッドの型パラメーター) を含むシグネチャが存在する MethodSpec です。

metadataToken は、現在のモジュールのスコープで有効なトークンではありません。

次の例では、 メソッドの ResolveMethod 2 つのオーバーロードを使用して、ジェネリック コンテキストと非ジェネリック コンテキストの呼び出しサイトからメタデータ トークンを解決する方法を示します。

このコード例では、 G1<Tg1> ジェネリック メソッドを持つ 2 つのジェネリック型と G2<Tg2> (G1(Of Tg1)G2(Of Tg2) Visual Basic の場合) を定義しています。 G1<Tg1> には、そのパラメーターに型パラメーター Tg1 を使用する非ジェネリック メソッドもあります。 型G2<Tg2>のジェネリック メソッドGM2<Tgm2>には、いくつかのメソッド呼び出しが含まれています。

  • ケース 1: ジェネリック メソッドGM1<Tgm1>は、 および GM2<Tgm2> の型パラメーターG2<Tg2>を型引数として使用して呼び出されます。 言い換えると、呼び出されるメソッドのパラメーター型は、 の型定義 G2<Tg2>から閉じたジェネリック型を構築するために使用される型によって異なります。

  • ケース 2: 非ジェネリック メソッド M1 が呼び出されます。 このメソッドの パラメーターでは、定義する型 の型パラメーター を使用します。この場合は、 G1<Tg1>外側の型 G2<Tg2>の型パラメーター である に置き換えられます。

  • ケース 3: ジェネリック メソッドGM1<Tgm1>が呼び出され、ジェネリック型とObjectジェネリック メソッドの型引数に それぞれ と が指定Int32されます。 このメソッド呼び出しは、外側の型またはメソッドの型パラメーターに依存しません。

  • ケース 4: クラスの非ジェネリック メソッド M1Example 呼び出されます。 このメソッド呼び出しは、外側の型またはメソッドの型パラメーターに依存しません。

さらに、この例では非ジェネリック Example クラスを定義します。 このクラスには、ジェネリック メソッド M の呼び出しを行うメソッドがあります。

  • ケース 5: ジェネリック メソッドGM1が呼び出され、ジェネリック型とObjectジェネリック メソッドの型引数に それぞれ と が指定Int32されます。 このメソッドのコンテキストには、ジェネリック型またはジェネリック メソッドを囲むがありません。

各ケースについて、この例では最初に呼び出されたメソッドを表す をMethodInfo構築し、 メソッドオーバーロードを使用して ResolveMethod(Int32, Type[], Type[]) トークンを解決します。次にType.GetGenericArguments、 メソッドと MethodInfo.GetGenericArguments メソッドを使用して および genericMethodArguments パラメーターの値をgenericTypeArguments取得します。 メソッドは非ジェネリック コンテキストに対して を返 Type.EmptyTypes すので、この手法はすべてのケースで機能します。 この例では、解決された を構築された MethodInfoMethodInfoと比較します。

次に、メソッド オーバーロードを ResolveMethod(Int32) 使用してトークンを解決します。 これは、メソッド呼び出しがジェネリック コンテキストに依存しないため、ケース 3、4、5 で機能します。 ケース 1 と 2 では、トークンを解決するための情報が不足しているため、例外がスローされます。

メタデータ トークンの値は、列挙型としてハードコーディングされます。 このコード例を変更すると、トークン値が変更される可能性があります。 新しいトークン値を確認するには、コードをコンパイルし、 /TOKENS オプションと共に Ildasm.exe を使用してアセンブリを調べます。 トークンは、呼び出しの時点で確認できます。 列挙に新しい値を挿入し、例を再コンパイルします。

C#
using System;
using System.Reflection;

namespace ResolveMethodExample
{
    // Metadata tokens for the MethodRefs that are to be resolved.
    // If you change this program, some or all of these metadata tokens might
    // change. The new token values can be discovered by compiling the example
    // and examining the assembly with Ildasm.exe, using the /TOKENS option.
    // Recompile the program after correcting the token values.
    enum Tokens
    {
        Case1 = 0x2b000001,
        Case2 = 0x0A000006,
        Case3 = 0x2b000002,
        Case4 = 0x06000006,
        Case5 = 0x2b000002
    }

    class G1<Tg1>
    {
        public void GM1<Tgm1> (Tg1 param1, Tgm1 param2) {}
        public void M1(Tg1 param) {}
    }

    class G2<Tg2>
    {
        public void GM2<Tgm2> (Tg2 param1, Tgm2 param2)
        {
            // Case 1: A generic method call that depends on its generic
            // context, because it uses the type parameters of the enclosing
            // generic type G2 and the enclosing generic method GM2. The token
            // for the MethodSpec is Tokens.Case1.
            G1<Tg2> g = new G1<Tg2>();
            g.GM1<Tgm2>(param1, param2);

            // Case 2: A non-generic method call that depends on its generic
            // context, because it uses the type parameter of the enclosing
            // generic type G2. The token for the MemberRef is Tokens.Case2.
            g.M1(param1);

            // Case 3: A generic method call that does not depend on its generic
            // context, because it does not use type parameters of the enclosing
            // generic type or method. The token for the MethodSpec is Tokens.Case3.
            G1<int> gi = new G1<int>();
            gi.GM1<object>(42, new Object());

            // Case 4: A non-generic method call that does not depend on its
            // generic context, because it does not use the type parameters of the
            // enclosing generic type or method. The token for the MethodDef is
            // Tokens.Case4.
            Example e = new Example();
            e.M();
        }
    }

    class Example
    {
        public void M()
        {
            G1<int> g = new G1<int>();
            // Case 5: A generic method call that does not have any generic
            // context. The token for the MethodSpec is Tokens.Case5.
            g.GM1<object>(42, new Object());
        }

        static void Main ()
        {
            Module mod = typeof(Example).Assembly.ManifestModule;
            MethodInfo miResolved2 = null;

            // Case 1: A generic method call that is dependent on its generic context.
            //
            // Create and display a MethodInfo representing the MethodSpec of the
            // generic method g.GM1<Tgm2>() that is called in G2<Tg2>.GM2<Tgm2>().
            Type t = typeof(G1<>).MakeGenericType(typeof(G2<>).GetGenericArguments());
            MethodInfo mi = typeof(G2<>).GetMethod("GM2");
            MethodInfo miTest = t.GetMethod("GM1").MakeGenericMethod(mi.GetGenericArguments());
            Console.WriteLine("\nCase 1:\n{0}", miTest);

            // Resolve the MethodSpec token for method G1<Tg2>.GM1<Tgm2>(), which
            // is called in method G2<Tg2>.GM2<Tgm2>(). The GetGenericArguments method
            // must be used to obtain the context for resolving the method.
            MethodInfo miResolved = (MethodInfo) mod.ResolveMethod(
                (int)Tokens.Case1,
                typeof(G2<>).GetGenericArguments(),
                typeof(G2<>).GetMethod("GM2").GetGenericArguments());
            Console.WriteLine(miResolved);
            Console.WriteLine("Is the resolved method the same? {0}", miResolved == miTest);

            // The overload that doesn't specify generic context throws an exception
            // because there is insufficient context to resolve the token.
            try
            {
                miResolved2 = (MethodInfo) mod.ResolveMethod((int)Tokens.Case1);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}: {1}", ex.GetType(), ex.Message);
            }

            // Case 2: A non-generic method call that is dependent on its generic context.
            //
            // Create and display a MethodInfo representing the MemberRef of the
            // non-generic method g.M1() that is called in G2<Tg2>.GM2<Tgm2>().
            t = typeof(G1<>).MakeGenericType(typeof(G2<>).GetGenericArguments());
            miTest = t.GetMethod("M1");
            Console.WriteLine("\nCase 2:\n{0}", miTest);

            // Resolve the MemberRef token for method G1<Tg2>.M1(), which is
            // called in method G2<Tg2>.GM2<Tgm2>(). The GetGenericArguments method
            // must be used to obtain the context for resolving the method, because
            // the method parameter comes from the generic type G1, and the type
            // argument, Tg2, comes from the generic type that encloses the call.
            // There is no enclosing generic method, so the value Type.EmptyTypes
            // could be passed for the genericMethodArguments parameter.
            miResolved = (MethodInfo) mod.ResolveMethod(
                (int)Tokens.Case2,
                typeof(G2<>).GetGenericArguments(),
                typeof(G2<>).GetMethod("GM2").GetGenericArguments());
            Console.WriteLine(miResolved);
            Console.WriteLine("Is the resolved method the same? {0}", miResolved == miTest);

            // The overload that doesn't specify generic context throws an exception
            // because there is insufficient context to resolve the token.
            try
            {
                miResolved2 = (MethodInfo) mod.ResolveMethod((int)Tokens.Case2);
            }
            catch (Exception ex)
            {
                Console.WriteLine("{0}: {1}", ex.GetType(), ex.Message);
            }

            // Case 3: A generic method call that is independent of its generic context.
            //
            // Create and display a MethodInfo representing the MethodSpec of the
            // generic method gi.GM1<object>() that is called in G2<Tg2>.GM2<Tgm2>().
            mi = typeof(G1<int>).GetMethod("GM1");
            miTest = mi.MakeGenericMethod(new Type[] { typeof(object) });
            Console.WriteLine("\nCase 3:\n{0}", miTest);

            // Resolve the token for method G1<int>.GM1<object>(), which is called
            // in G2<Tg2>.GM2<Tgm2>(). The GetGenericArguments method is used to
            // obtain the context for resolving the method, but the method call in
            // this case does not use type parameters of the enclosing type or
            // method, so Type.EmptyTypes could be used for both arguments.
            miResolved = (MethodInfo) mod.ResolveMethod(
                (int)Tokens.Case3,
                typeof(G2<>).GetGenericArguments(),
                typeof(G2<>).GetMethod("GM2").GetGenericArguments());
            Console.WriteLine(miResolved);
            Console.WriteLine("Is the resolved method the same? {0}", miResolved == miTest);

            // The method call in this case does not depend on the enclosing generic
            // context, so the token can also be resolved by the simpler overload.
            miResolved2 = (MethodInfo) mod.ResolveMethod((int)Tokens.Case3);

            // Case 4: A non-generic method call that is independent of its generic context.
            //
            // Create and display a MethodInfo representing the MethodDef of the
            // method e.M() that is called in G2<Tg2>.GM2<Tgm2>().
            miTest = typeof(Example).GetMethod("M");
            Console.WriteLine("\nCase 4:\n{0}", miTest);

            // Resolve the token for method Example.M(), which is called in
            // G2<Tg2>.GM2<Tgm2>(). The GetGenericArguments method is used to
            // obtain the context for resolving the method, but the non-generic
            // method call does not use type parameters of the enclosing type or
            // method, so Type.EmptyTypes could be used for both arguments.
            miResolved = (MethodInfo) mod.ResolveMethod(
                (int)Tokens.Case4,
                typeof(G2<>).GetGenericArguments(),
                typeof(G2<>).GetMethod("GM2").GetGenericArguments());
            Console.WriteLine(miResolved);
            Console.WriteLine("Is the resolved method the same? {0}", miResolved == miTest);

            // The method call in this case does not depend on any enclosing generic
            // context, so the token can also be resolved by the simpler overload.
            miResolved2 = (MethodInfo) mod.ResolveMethod((int)Tokens.Case4);

            // Case 5: Generic method call in a non-generic context.
            //
            // Create and display a MethodInfo representing the MethodRef of the
            // closed generic method g.GM1<object>() that is called in Example.M().
            mi = typeof(G1<int>).GetMethod("GM1");
            miTest = mi.MakeGenericMethod(new Type[] { typeof(object) });
            Console.WriteLine("\nCase 5:\n{0}", miTest);

            // Resolve the token for method G1<int>.GM1<object>(), which is called
            // in method Example.M(). The GetGenericArguments method is used to
            // obtain the context for resolving the method, but the enclosing type
            // and method are not generic, so Type.EmptyTypes could be used for
            // both arguments.
            miResolved = (MethodInfo) mod.ResolveMethod(
                (int)Tokens.Case5,
                typeof(Example).GetGenericArguments(),
                typeof(Example).GetMethod("M").GetGenericArguments());
            Console.WriteLine(miResolved);
            Console.WriteLine("Is the resolved method the same? {0}", miResolved == miTest);

            // The method call in this case does not depend on any enclosing generic
            // context, so the token can also be resolved by the simpler overload.
            miResolved2 = (MethodInfo) mod.ResolveMethod((int)Tokens.Case5);
        }
    }
}
/* This example produces the following output:

Case 1:
Void GM1[Tgm2](Tg2, Tgm2)
Void GM1[Tgm2](Tg2, Tgm2)
Is the resolved method the same? True
System.ArgumentException: A BadImageFormatException has been thrown while parsing the signature. This is likely due to lack of a generic context. Ensure genericTypeArguments and genericMethodArguments are provided and contain enough context.

Case 2:
Void M1(Tg2)
Void M1(Tg2)
Is the resolved method the same? True
System.ArgumentException: A BadImageFormatException has been thrown while parsing the signature. This is likely due to lack of a generic context. Ensure genericTypeArguments and genericMethodArguments are provided and contain enough context.

Case 3:
Void GM1[Object](Int32, System.Object)
Void GM1[Object](Int32, System.Object)
Is the resolved method the same? True

Case 4:
Void M()
Void M()
Is the resolved method the same? True

Case 5:
Void GM1[Object](Int32, System.Object)
Void GM1[Object](Int32, System.Object)
Is the resolved method the same? True
 */

注釈

シグネチャに要素型または が含まれている のMethodSpecメタデータ トークンを解決するには、 メソッドオーバーロードをResolveMethod(Int32, Type[], Type[])使用します。これにより、必要なコンテキストを指定できます。ELEMENT_TYPE_MVARELEMENT_TYPE_VAR つまり、ジェネリック型のジェネリック型パラメーターまたはトークンが埋め込まれているジェネリック メソッドに依存するメソッドのメタデータ トークンを解決する場合は、これらの型パラメーターを指定できるオーバーロードを使用する必要があります。

注意

メタデータ トークンに関する情報は、共通言語インフラストラクチャ (CLI) のドキュメント(特にパーティション II: メタデータ定義とセマンティクス)にあります。 詳細については、「 ECMA 335 共通言語インフラストラクチャ (CLI)」を参照してください。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.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