Module.ResolveMethod 메서드

정의

메타데이터 토큰으로 식별되는 메서드를 반환합니다.Returns the method identified by a metadata token.

오버로드

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

메서드 또는 컨텍스트에서 지정 된 제네릭 형식 매개 변수로 정의 된 지정 된 메타 데이터 토큰으로 식별 하는 생성자를 반환 합니다.Returns the method or constructor identified by the specified metadata token, in the context defined by the specified generic type parameters.

ResolveMethod(Int32)

메서드 또는 지정 된 메타 데이터 토큰으로 식별 되는 생성자를 반환 합니다.Returns the method or constructor identified by the specified metadata token.

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

메서드 또는 컨텍스트에서 지정 된 제네릭 형식 매개 변수로 정의 된 지정 된 메타 데이터 토큰으로 식별 하는 생성자를 반환 합니다.Returns the method or constructor identified by the specified metadata token, in the context defined by the specified generic type parameters.

public:
 System::Reflection::MethodBase ^ ResolveMethod(int metadataToken, cli::array <Type ^> ^ genericTypeArguments, cli::array <Type ^> ^ genericMethodArguments);
public System.Reflection.MethodBase ResolveMethod (int metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments);
member this.ResolveMethod : int * Type[] * Type[] -> System.Reflection.MethodBase
Public Function ResolveMethod (metadataToken As Integer, genericTypeArguments As Type(), genericMethodArguments As Type()) As MethodBase

매개 변수

metadataToken
Int32

메서드 또는 모듈의 생성자를 식별 하는 메타 데이터 토큰입니다.A metadata token that identifies a method or constructor in the module.

genericTypeArguments
Type[]

해당 토큰이 범위 내에 있는 형식의 제네릭 형식 인수를 나타내는 Type 개체의 배열이거나, 형식이 제네릭이 아닌 경우에는 null입니다.An array of Type objects representing the generic type arguments of the type where the token is in scope, or null if that type is not generic.

genericMethodArguments
Type[]

해당 토큰이 범위 내에 있는 메서드의 제네릭 형식 인수를 나타내는 Type 개체의 배열이거나, 메서드가 제네릭이 아닌 경우에는 null입니다.An array of Type objects representing the generic type arguments of the method where the token is in scope, or null if that method is not generic.

반환

지정된 메타데이터 토큰으로 식별되는 메서드를 나타내는 MethodBase 개체입니다.A MethodBase object representing the method that is identified by the specified metadata token.

예외

metadataToken이 현재 모듈의 범위에 있는 메서드나 생성자의 토큰이 아닌 경우metadataToken is not a token for a method or constructor in the scope of the current module.

-또는--or- metadataTokenMethodSpec(제네릭 형식의 형식 매개 변수) 또는 var(제네릭 메서드의 형식 매개 변수) 요소 형식이 포함된 시그니처를 사용하는 mvar이고, genericTypeArgumentsgenericMethodArguments 중 하나 또는 둘 다에 대해 필요한 제네릭 형식 인수를 제공하지 않은 경우metadataToken is a MethodSpec whose signature contains element type var (a type parameter of a generic type) or mvar (a type parameter of a generic method), and the necessary generic type arguments were not supplied for either or both of genericTypeArguments and genericMethodArguments.

metadataToken이 현재 모듈의 범위에 있는 유효한 토큰이 아닌 경우metadataToken is not a valid token in the scope of the current module.

예제

다음 예제에서는 ResolveMethod 메서드의 두 오버 로드를 사용 하 여 제네릭 및 제네릭이 아닌 컨텍스트의 호출 사이트에서 메타 데이터 토큰을 확인 하는 방법을 보여 줍니다.The following example shows how to use the two overloads of the ResolveMethod method to resolve metadata tokens from call sites in generic and non-generic contexts.

이 코드 예제에서는 제네릭 메서드를 포함 하는 두 개의 제네릭 형식 G1<Tg1>G2<Tg2>을 정의 합니다.The code example defines two generic types, G1<Tg1> and G2<Tg2>, each of which has a generic method. 또한 G1<Tg1>에는 매개 변수에 대해 Tg1 형식 매개 변수를 사용 하는 제네릭이 아닌 메서드가 있습니다.G1<Tg1> also has a non-generic method that uses the type parameter Tg1 for its parameter. 제네릭 메서드는 다음과 같은 몇 가지 메서드 호출을 포함 G2<Tg2> 형식으로 GM2<Tgm2>.The generic method GM2<Tgm2> in type G2<Tg2> contains several method calls:

  • 사례 1: G2<Tg2>의 형식 매개 변수와 GM2<Tgm2> 형식 인수로 사용 하 여 제네릭 메서드 GM1<Tgm1>를 호출 합니다.Case 1: The generic method GM1<Tgm1> is called, using the type parameters of G2<Tg2> and GM2<Tgm2> as the type arguments. 즉, 호출 된 메서드의 매개 변수 형식은 G2<Tg2>에 대 한 형식 정의에서 폐쇄형 제네릭 형식을 생성 하는 데 사용 되는 형식에 따라 달라 집니다.In other words, the parameter types of the called method will depend on the types that are used to construct a closed generic type from the type definition for G2<Tg2>.

  • 사례 2: 제네릭이 아닌 메서드 M1를 호출 합니다.Case 2: The non-generic method M1 is called. 이 메서드의 매개 변수는 정의 형식 G1<Tg1>의 형식 매개 변수를 사용 합니다 .이 경우이 경우에는 바깥쪽 형식의 형식 매개 변수로 대체 됩니다 G2<Tg2>.The parameter of this method uses the type parameter of the defining type, G1<Tg1>, which is replaced in this case by the type parameter of the enclosing type, G2<Tg2>.

  • 사례 3: 제네릭 GM1<Tgm1> 메서드를 호출 하 여 제네릭 형식 및 제네릭 메서드의 형식 인수에 대해 Int32Object를 지정 합니다.Case 3: The generic method GM1<Tgm1> is called, specifying Int32 and Object for the type arguments of the generic type and the generic method, respectively. 이 메서드 호출은 바깥쪽 형식 또는 메서드의 형식 매개 변수에 종속 되지 않습니다.This method call does not depend on the type parameters of the enclosing type or method.

  • 사례 4: Example 클래스 M1 제네릭이 아닌 메서드를 호출 합니다.Case 4: The non-generic method M1 of the Example class is called. 이 메서드 호출은 바깥쪽 형식 또는 메서드의 형식 매개 변수에 종속 되지 않습니다.This method call does not depend on the type parameters of the enclosing type or method.

또한이 예제에서는 제네릭이 아닌 Example 클래스를 정의 합니다.In addition, the example defines the non-generic Example class. 이 클래스에는 제네릭 메서드를 호출 하는 메서드가 M 있습니다.This class has a method M that makes a call to a generic method.

  • 사례 5: 제네릭 메서드 GM1<Tgm1>를 호출 하 여 제네릭 형식 및 제네릭 메서드의 형식 인수에 대 한 Int32Object를 각각 지정 합니다.Case 5: The generic method GM1<Tgm1> is called, specifying Int32 and Object for the type arguments of the generic type and the generic method, respectively. 이 메서드에 대 한 컨텍스트는 없는 바깥쪽 제네릭 형식 또는 제네릭 메서드.The context for this method has no enclosing generic type or generic method.

각 경우에 대 한 예제에서는 먼저 호출 된 메서드를 나타내는 MethodInfo를 생성 한 다음 Type.GetGenericArgumentsMethodInfo.GetGenericArguments 메서드를 사용 하 여 genericTypeArgumentsgenericMethodArguments 매개 변수에 대 한 값을 가져오는 ResolveMethod(Int32, Type[], Type[]) 메서드 오버 로드를 사용 하 여 토큰을 확인 합니다.For each case, the example first constructs a MethodInfo that represents the called method, and then resolves the token using the ResolveMethod(Int32, Type[], Type[]) method overload, using the Type.GetGenericArguments and MethodInfo.GetGenericArguments methods to get the values for the genericTypeArguments and genericMethodArguments parameters. 이 기술은 메서드가 제네릭이 아닌 컨텍스트에 대해 Type.EmptyTypes를 반환 하기 때문에 모든 경우에 작동 합니다.This technique works in all cases, because the methods return Type.EmptyTypes for non-generic contexts. 이 예제에서는 확인 된 MethodInfo를 생성 된 MethodInfo와 비교 합니다.The example compares the resolved MethodInfo with the constructed MethodInfo.

그런 다음이 예제에서는 ResolveMethod(Int32) 메서드 오버 로드를 사용 하 여 토큰을 확인 하려고 시도 합니다.The example then attempts to use the ResolveMethod(Int32) method overload to resolve the token. 이 메서드 호출에서 제네릭 컨텍스트에 종속 되지 않는 때문에 3, 4 및 5의 경우 작동 합니다.This works in cases 3, 4, and 5, because the method calls do not depend on the generic context. 1과 2의 경우에서 토큰을 확인 하는 데 필요한 정보가 부족 있기 때문에 예외가 throw 됩니다.In cases 1 and 2, an exception is thrown because there is insufficient information to resolve the token.

메타 데이터 토큰 값을 열거형으로 하드 코드 됩니다.The metadata token values are hard-coded as an enumeration. 이 코드 예제를 변경 하는 경우 토큰 값을 변경 하려면 가능성이 있습니다.If you change this code example, the token values are likely to change. 새 토큰 값을 확인 하려면 코드를 컴파일하고 /TOKENS 옵션과 함께 ildasm.exe를 사용 하 여 어셈블리를 검사 합니다.To determine the new token values, compile the code and use Ildasm.exe with the /TOKENS option to examine the assembly. 호출 지점에서 토큰을 찾을 수 있습니다.The tokens can be found at the points of call. 열거형에 새 값을 삽입 하 고 예제를 다시 컴파일하십시오.Insert the new values into the enumeration, and recompile the example.

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
 */
Imports 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 = &H2B000003
        Case2 = &HA00001F
        Case3 = &H2B000004
        Case4 = &H6000017
        Case5 = &H2B000004
    End Enum 


    Class G1(Of Tg1)

        Public Sub GM1(Of Tgm1) (ByVal param1 As Tg1, ByVal param2 As Tgm1)
        End Sub

        Public Sub M1(ByVal param As Tg1)
        End Sub
    End Class

    Class G2(Of Tg2)
    
        Public Sub GM2(Of Tgm2) (ByVal param1 As Tg2, ByVal param2 As Tgm2)
        
            ' 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.
            Dim g As New G1(Of Tg2)()
            g.GM1(Of 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.
            Dim gi As New G1(Of Integer)()
            gi.GM1(Of 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.
            Dim e As New Example()
            e.M()
        End Sub 
    End Class

    Class Example
        Public Sub M() 
            Dim g As New G1(Of Integer)()
            ' Case 5: A generic method call that does not have any generic 
            ' context. The token for the MethodSpec is Tokens.Case5.
            g.GM1(Of Object)(42, New Object())
        End Sub 
    
        Shared Sub Main() 
            Dim m As [Module] = GetType(Example).Assembly.ManifestModule
            Dim miResolved2 As MethodInfo = Nothing
        
            ' 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(Of Tgm2)() that is called in G2(Of Tg2).GM2(Of Tgm2)().
            Dim t As Type = GetType(G1(Of )).MakeGenericType(GetType(G2(Of )).GetGenericArguments())
            Dim mi As MethodInfo = GetType(G2(Of )).GetMethod("GM2")
            Dim miTest As MethodInfo = t.GetMethod("GM1").MakeGenericMethod(mi.GetGenericArguments())
            Console.WriteLine(vbCrLf & "Case 1:" & vbCrLf & miTest.ToString())
        
            ' Resolve the MethodSpec token for method G1(Of Tg2).GM1(Of Tgm2)(), which 
            ' is called in method G2(Of Tg2).GM2(Of Tgm2)(). The GetGenericArguments 
            ' method must be used to obtain the context for resolving the method.
            Dim miResolved As MethodInfo = CType(m.ResolveMethod( _
                    CInt(Tokens.Case1), _
                    GetType(G2(Of )).GetGenericArguments(), _
                    GetType(G2(Of )).GetMethod("GM2").GetGenericArguments()), _
                MethodInfo)
            Console.WriteLine(miResolved)
            Console.WriteLine("Is the resolved method the same? {0}", miResolved Is miTest)
        
            ' The overload that doesn't specify generic context throws an exception
            ' because there is insufficient context to resolve the token.
            Try
                miResolved2 = CType(m.ResolveMethod(CInt(Tokens.Case1)), MethodInfo)
            Catch ex As Exception
                Console.WriteLine("{0}: {1}", ex.GetType(), ex.Message)
            End Try
        
        
            ' 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(Of Tg2).GM2(Of Tgm2)().
            t = GetType(G1(Of )).MakeGenericType(GetType(G2(Of )).GetGenericArguments())
            miTest = t.GetMethod("M1")
            Console.WriteLine(vbCrLf & "Case 2:" & vbCrLf & miTest.ToString())
        
            ' Resolve the MemberRef token for method G1(Of Tg2).M1(), which is
            ' called in method G2(Of Tg2).GM2(Of 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 
            ' because argument, Tg2, comes from the generic type that encloses the 
            ' call. There is no enclosing generic method, so Type.EmptyTypes could
            ' be passed for the genericMethodArguments parameter.
            miResolved = CType(m.ResolveMethod( _
                    CInt(Tokens.Case2), _
                    GetType(G2(Of )).GetGenericArguments(), _
                    GetType(G2(Of )).GetMethod("GM2").GetGenericArguments()), _
                MethodInfo)
            Console.WriteLine(miResolved)
            Console.WriteLine("Is the resolved method the same? {0}", miResolved Is miTest)
        
            ' The overload that doesn't specify generic context throws an exception
            ' because there is insufficient context to resolve the token.
            Try
                miResolved2 = CType(m.ResolveMethod(CInt(Tokens.Case2)), MethodInfo)
            Catch ex As Exception
                Console.WriteLine("{0}: {1}", ex.GetType(), ex.Message)
            End Try
        
        
            ' 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(Of Object)() that is called in G2(Of Tg2).GM2(Of Tgm2)().
            mi = GetType(G1(Of Integer)).GetMethod("GM1")
            miTest = mi.MakeGenericMethod(New Type() {GetType(Object)})
            Console.WriteLine(vbCrLf & "Case 3:" & vbCrLf & miTest.ToString())
        
            ' Resolve the token for method G1(Of Integer).GM1(Of Object)(), which is 
            ' calledin G2(Of Tg2).GM2(Of 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 = CType(m.ResolveMethod( _
                    CInt(Tokens.Case3), _
                    GetType(G2(Of )).GetGenericArguments(), _
                    GetType(G2(Of )).GetMethod("GM2").GetGenericArguments()), _
                MethodInfo)
            Console.WriteLine(miResolved)
            Console.WriteLine("Is the resolved method the same? {0}", miResolved Is 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 = CType(m.ResolveMethod(CInt(Tokens.Case3)), MethodInfo)
        
        
            ' 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(Of Tg2).GM2(Of Tgm2)().
            miTest = GetType(Example).GetMethod("M")
            Console.WriteLine(vbCrLf & "Case 4:" & vbCrLf & miTest.ToString())
        
            ' Resolve the token for method Example.M(), which is called in
            ' G2(Of Tg2).GM2(Of 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 = CType(m.ResolveMethod( _
                    CInt(Tokens.Case4), _
                    GetType(G2(Of )).GetGenericArguments(), _
                    GetType(G2(Of )).GetMethod("GM2").GetGenericArguments()), _
                MethodInfo)
            Console.WriteLine(miResolved)
            Console.WriteLine("Is the resolved method the same? {0}", miResolved Is 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 = CType(m.ResolveMethod(CInt(Tokens.Case4)), MethodInfo)
        
        
            ' 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(Of Object)() that is called in Example.M().
            mi = GetType(G1(Of Integer)).GetMethod("GM1")
            miTest = mi.MakeGenericMethod(New Type() {GetType(Object)})
            Console.WriteLine(vbCrLf & "Case 5:" & vbCrLf & miTest.ToString())
        
            ' Resolve the token for method G1(Of Integer).GM1(Of 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 = CType(m.ResolveMethod( _
                    CInt(Tokens.Case5), _
                    GetType(Example).GetGenericArguments(), _
                    GetType(Example).GetMethod("M").GetGenericArguments()), _
                MethodInfo)
            Console.WriteLine(miResolved)
            Console.WriteLine("Is the resolved method the same? {0}", miResolved Is 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 = CType(m.ResolveMethod(CInt(Tokens.Case5)), MethodInfo)
    
        End Sub 
    End Class 
End Namespace

' 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
' 

설명

genericTypeArguments에 대 한 제네릭 형식 인수의 배열을 가져오려면 metadataToken 범위에 있는 형식에 대해 Type.GetGenericArguments 메서드를 사용 합니다.Use the Type.GetGenericArguments method on the type where metadataToken is in scope to obtain an array of generic type arguments for genericTypeArguments. genericMethodArguments에 대 한 제네릭 형식 인수의 배열을 가져오려면 metadataToken 범위에 있는 메서드에서 MethodInfo.GetGenericArguments 메서드를 사용 합니다.Use the MethodInfo.GetGenericArguments method on the method where metadataToken is in scope to obtain an array of generic type arguments for genericMethodArguments. 항상 것은 안전 필요 하지 않은 경우에 이러한 인수를 제공 합니다.It is always safe to provide these arguments, even when they are not needed.

참고

공용 언어 인프라 (CLI) 설명서에서 "II: 메타 데이터 정의 및 의미" 메타 데이터 토큰에 대 한 정보를 찾을 수 있습니다.Information about metadata tokens can be found in the Common Language Infrastructure (CLI) documentation, especially "Partition II: Metadata Definition and Semantics". 설명서는 온라인으로 제공 됩니다. ECMA 웹 사이트의 MSDN 및 표준 ecma-335-CLI (공용 언어 인프라) 에서 Ecma C# 및 공용 언어 인프라 표준 을 참조 하세요.The documentation is available online; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the ECMA Web site.

ResolveMethod(Int32)

메서드 또는 지정 된 메타 데이터 토큰으로 식별 되는 생성자를 반환 합니다.Returns the method or constructor identified by the specified metadata token.

public:
 System::Reflection::MethodBase ^ ResolveMethod(int metadataToken);
public System.Reflection.MethodBase ResolveMethod (int metadataToken);
member this.ResolveMethod : int -> System.Reflection.MethodBase
Public Function ResolveMethod (metadataToken As Integer) As MethodBase

매개 변수

metadataToken
Int32

메서드 또는 모듈의 생성자를 식별 하는 메타 데이터 토큰입니다.A metadata token that identifies a method or constructor in the module.

반환

지정된 메타데이터 토큰으로 식별되는 메서드나 생성자를 나타내는 MethodBase 개체입니다.A MethodBase object representing the method or constructor that is identified by the specified metadata token.

예외

metadataToken이 현재 모듈의 범위에 있는 메서드나 생성자의 토큰이 아닌 경우metadataToken is not a token for a method or constructor in the scope of the current module.

-또는--or- metadataTokenMethodSpec(제네릭 형식의 형식 매개 변수) 또는 var(제네릭 메서드의 형식 매개 변수) 요소 형식이 포함된 시그니처를 사용하는 mvar인 경우metadataToken is a MethodSpec whose signature contains element type var (a type parameter of a generic type) or mvar (a type parameter of a generic method).

metadataToken이 현재 모듈의 범위에 있는 유효한 토큰이 아닌 경우metadataToken is not a valid token in the scope of the current module.

예제

다음 예제에서는 ResolveMethod 메서드의 두 오버 로드를 사용 하 여 제네릭 및 제네릭이 아닌 컨텍스트의 호출 사이트에서 메타 데이터 토큰을 확인 하는 방법을 보여 줍니다.The following example shows how to use the two overloads of the ResolveMethod method to resolve metadata tokens from call sites in generic and non-generic contexts.

이 코드 예제에서는 각각 제네릭 메서드를 포함 하는 두 개의 제네릭 형식 G1<Tg1>G2<Tg2> (Visual Basic의G1(Of Tg1)G2(Of Tg2))를 정의 합니다.The code example defines two generic types, G1<Tg1> and G2<Tg2> (G1(Of Tg1) and G2(Of Tg2) in Visual Basic), each of which has a generic method. 또한 G1<Tg1>에는 매개 변수에 대해 Tg1 형식 매개 변수를 사용 하는 제네릭이 아닌 메서드가 있습니다.G1<Tg1> also has a non-generic method that uses the type parameter Tg1 for its parameter. 제네릭 메서드는 다음과 같은 몇 가지 메서드 호출을 포함 G2<Tg2> 형식으로 GM2<Tgm2>.The generic method GM2<Tgm2> in type G2<Tg2> contains several method calls:

  • 사례 1: G2<Tg2>의 형식 매개 변수와 GM2<Tgm2> 형식 인수로 사용 하 여 제네릭 메서드 GM1<Tgm1>를 호출 합니다.Case 1: The generic method GM1<Tgm1> is called, using the type parameters of G2<Tg2> and GM2<Tgm2> as the type arguments. 즉, 호출 된 메서드의 매개 변수 형식은 G2<Tg2>에 대 한 형식 정의에서 폐쇄형 제네릭 형식을 생성 하는 데 사용 되는 형식에 따라 달라 집니다.In other words, the parameter types of the called method will depend on the types that are used to construct a closed generic type from the type definition for G2<Tg2>.

  • 사례 2: 제네릭이 아닌 메서드 M1를 호출 합니다.Case 2: The non-generic method M1 is called. 이 메서드의 매개 변수는 정의 형식 G1<Tg1>의 형식 매개 변수를 사용 합니다 .이 경우이 경우에는 바깥쪽 형식의 형식 매개 변수로 대체 됩니다 G2<Tg2>.The parameter of this method uses the type parameter of the defining type, G1<Tg1>, which is replaced in this case by the type parameter of the enclosing type, G2<Tg2>.

  • 사례 3: 제네릭 GM1<Tgm1> 메서드를 호출 하 여 제네릭 형식 및 제네릭 메서드의 형식 인수에 대해 Int32Object를 지정 합니다.Case 3: The generic method GM1<Tgm1> is called, specifying Int32 and Object for the type arguments of the generic type and the generic method, respectively. 이 메서드 호출은 바깥쪽 형식 또는 메서드의 형식 매개 변수에 종속 되지 않습니다.This method call does not depend on the type parameters of the enclosing type or method.

  • 사례 4: Example 클래스 M1 제네릭이 아닌 메서드를 호출 합니다.Case 4: The non-generic method M1 of the Example class is called. 이 메서드 호출은 바깥쪽 형식 또는 메서드의 형식 매개 변수에 종속 되지 않습니다.This method call does not depend on the type parameters of the enclosing type or method.

또한이 예제에서는 제네릭이 아닌 Example 클래스를 정의 합니다.In addition, the example defines the non-generic Example class. 이 클래스에는 제네릭 메서드를 호출 하는 메서드가 M 있습니다.This class has a method M that makes a call to a generic method:

  • 사례 5: 제네릭 메서드 GM1를 호출 하 여 제네릭 형식 및 제네릭 메서드의 형식 인수에 대 한 Int32Object를 각각 지정 합니다.Case 5: The generic method GM1 is called, specifying Int32 and Object for the type arguments of the generic type and the generic method, respectively. 이 메서드에 대 한 컨텍스트는 없는 바깥쪽 제네릭 형식 또는 제네릭 메서드.The context for this method has no enclosing generic type or generic method.

각 경우에 대 한 예제에서는 먼저 호출 된 메서드를 나타내는 MethodInfo를 생성 한 다음 Type.GetGenericArgumentsMethodInfo.GetGenericArguments 메서드를 사용 하 여 genericTypeArgumentsgenericMethodArguments 매개 변수에 대 한 값을 가져오는 ResolveMethod(Int32, Type[], Type[]) 메서드 오버 로드를 사용 하 여 토큰을 확인 합니다.For each case, the example first constructs a MethodInfo that represents the called method, and then resolves the token using the ResolveMethod(Int32, Type[], Type[]) method overload, using the Type.GetGenericArguments and MethodInfo.GetGenericArguments methods to get the values for the genericTypeArguments and genericMethodArguments parameters. 이 기술은 메서드가 제네릭이 아닌 컨텍스트에 대해 Type.EmptyTypes를 반환 하기 때문에 모든 경우에 작동 합니다.This technique works in all cases, because the methods return Type.EmptyTypes for non-generic contexts. 이 예제에서는 확인 된 MethodInfo를 생성 된 MethodInfo와 비교 합니다.The example compares the resolved MethodInfo with the constructed MethodInfo.

그런 다음이 예제에서는 ResolveMethod(Int32) 메서드 오버 로드를 사용 하 여 토큰을 확인 하려고 시도 합니다.The example then attempts to use the ResolveMethod(Int32) method overload to resolve the token. 이 메서드 호출에서 제네릭 컨텍스트에 종속 되지 않는 때문에 3, 4 및 5의 경우 작동 합니다.This works in cases 3, 4, and 5, because the method calls do not depend on the generic context. 1과 2의 경우에서 토큰을 확인 하는 데 필요한 정보가 부족 있기 때문에 예외가 throw 됩니다.In cases 1 and 2, an exception is thrown because there is insufficient information to resolve the token.

메타 데이터 토큰 값을 열거형으로 하드 코드 됩니다.The metadata token values are hard-coded as an enumeration. 이 코드 예제를 변경 하는 경우 토큰 값을 변경 하려면 가능성이 있습니다.If you change this code example, the token values are likely to change. 새 토큰 값을 확인 하려면 코드를 컴파일하고 /TOKENS 옵션과 함께 ildasm.exe를 사용 하 여 어셈블리를 검사 합니다.To determine the new token values, compile the code and use Ildasm.exe with the /TOKENS option to examine the assembly. 호출 지점에서 토큰을 찾을 수 있습니다.The tokens can be found at the points of call. 열거형에 새 값을 삽입 하 고 예제를 다시 컴파일하십시오.Insert the new values into the enumeration, and recompile the example.

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
 */
Imports 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 = &H2B000003
        Case2 = &HA00001F
        Case3 = &H2B000004
        Case4 = &H6000017
        Case5 = &H2B000004
    End Enum 


    Class G1(Of Tg1)

        Public Sub GM1(Of Tgm1) (ByVal param1 As Tg1, ByVal param2 As Tgm1)
        End Sub

        Public Sub M1(ByVal param As Tg1)
        End Sub
    End Class

    Class G2(Of Tg2)
    
        Public Sub GM2(Of Tgm2) (ByVal param1 As Tg2, ByVal param2 As Tgm2)
        
            ' 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.
            Dim g As New G1(Of Tg2)()
            g.GM1(Of 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.
            Dim gi As New G1(Of Integer)()
            gi.GM1(Of 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.
            Dim e As New Example()
            e.M()
        End Sub 
    End Class

    Class Example
        Public Sub M() 
            Dim g As New G1(Of Integer)()
            ' Case 5: A generic method call that does not have any generic 
            ' context. The token for the MethodSpec is Tokens.Case5.
            g.GM1(Of Object)(42, New Object())
        End Sub 
    
        Shared Sub Main() 
            Dim m As [Module] = GetType(Example).Assembly.ManifestModule
            Dim miResolved2 As MethodInfo = Nothing
        
            ' 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(Of Tgm2)() that is called in G2(Of Tg2).GM2(Of Tgm2)().
            Dim t As Type = GetType(G1(Of )).MakeGenericType(GetType(G2(Of )).GetGenericArguments())
            Dim mi As MethodInfo = GetType(G2(Of )).GetMethod("GM2")
            Dim miTest As MethodInfo = t.GetMethod("GM1").MakeGenericMethod(mi.GetGenericArguments())
            Console.WriteLine(vbCrLf & "Case 1:" & vbCrLf & miTest.ToString())
        
            ' Resolve the MethodSpec token for method G1(Of Tg2).GM1(Of Tgm2)(), which 
            ' is called in method G2(Of Tg2).GM2(Of Tgm2)(). The GetGenericArguments 
            ' method must be used to obtain the context for resolving the method.
            Dim miResolved As MethodInfo = CType(m.ResolveMethod( _
                    CInt(Tokens.Case1), _
                    GetType(G2(Of )).GetGenericArguments(), _
                    GetType(G2(Of )).GetMethod("GM2").GetGenericArguments()), _
                MethodInfo)
            Console.WriteLine(miResolved)
            Console.WriteLine("Is the resolved method the same? {0}", miResolved Is miTest)
        
            ' The overload that doesn't specify generic context throws an exception
            ' because there is insufficient context to resolve the token.
            Try
                miResolved2 = CType(m.ResolveMethod(CInt(Tokens.Case1)), MethodInfo)
            Catch ex As Exception
                Console.WriteLine("{0}: {1}", ex.GetType(), ex.Message)
            End Try
        
        
            ' 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(Of Tg2).GM2(Of Tgm2)().
            t = GetType(G1(Of )).MakeGenericType(GetType(G2(Of )).GetGenericArguments())
            miTest = t.GetMethod("M1")
            Console.WriteLine(vbCrLf & "Case 2:" & vbCrLf & miTest.ToString())
        
            ' Resolve the MemberRef token for method G1(Of Tg2).M1(), which is
            ' called in method G2(Of Tg2).GM2(Of 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 
            ' because argument, Tg2, comes from the generic type that encloses the 
            ' call. There is no enclosing generic method, so Type.EmptyTypes could
            ' be passed for the genericMethodArguments parameter.
            miResolved = CType(m.ResolveMethod( _
                    CInt(Tokens.Case2), _
                    GetType(G2(Of )).GetGenericArguments(), _
                    GetType(G2(Of )).GetMethod("GM2").GetGenericArguments()), _
                MethodInfo)
            Console.WriteLine(miResolved)
            Console.WriteLine("Is the resolved method the same? {0}", miResolved Is miTest)
        
            ' The overload that doesn't specify generic context throws an exception
            ' because there is insufficient context to resolve the token.
            Try
                miResolved2 = CType(m.ResolveMethod(CInt(Tokens.Case2)), MethodInfo)
            Catch ex As Exception
                Console.WriteLine("{0}: {1}", ex.GetType(), ex.Message)
            End Try
        
        
            ' 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(Of Object)() that is called in G2(Of Tg2).GM2(Of Tgm2)().
            mi = GetType(G1(Of Integer)).GetMethod("GM1")
            miTest = mi.MakeGenericMethod(New Type() {GetType(Object)})
            Console.WriteLine(vbCrLf & "Case 3:" & vbCrLf & miTest.ToString())
        
            ' Resolve the token for method G1(Of Integer).GM1(Of Object)(), which is 
            ' calledin G2(Of Tg2).GM2(Of 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 = CType(m.ResolveMethod( _
                    CInt(Tokens.Case3), _
                    GetType(G2(Of )).GetGenericArguments(), _
                    GetType(G2(Of )).GetMethod("GM2").GetGenericArguments()), _
                MethodInfo)
            Console.WriteLine(miResolved)
            Console.WriteLine("Is the resolved method the same? {0}", miResolved Is 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 = CType(m.ResolveMethod(CInt(Tokens.Case3)), MethodInfo)
        
        
            ' 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(Of Tg2).GM2(Of Tgm2)().
            miTest = GetType(Example).GetMethod("M")
            Console.WriteLine(vbCrLf & "Case 4:" & vbCrLf & miTest.ToString())
        
            ' Resolve the token for method Example.M(), which is called in
            ' G2(Of Tg2).GM2(Of 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 = CType(m.ResolveMethod( _
                    CInt(Tokens.Case4), _
                    GetType(G2(Of )).GetGenericArguments(), _
                    GetType(G2(Of )).GetMethod("GM2").GetGenericArguments()), _
                MethodInfo)
            Console.WriteLine(miResolved)
            Console.WriteLine("Is the resolved method the same? {0}", miResolved Is 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 = CType(m.ResolveMethod(CInt(Tokens.Case4)), MethodInfo)
        
        
            ' 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(Of Object)() that is called in Example.M().
            mi = GetType(G1(Of Integer)).GetMethod("GM1")
            miTest = mi.MakeGenericMethod(New Type() {GetType(Object)})
            Console.WriteLine(vbCrLf & "Case 5:" & vbCrLf & miTest.ToString())
        
            ' Resolve the token for method G1(Of Integer).GM1(Of 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 = CType(m.ResolveMethod( _
                    CInt(Tokens.Case5), _
                    GetType(Example).GetGenericArguments(), _
                    GetType(Example).GetMethod("M").GetGenericArguments()), _
                MethodInfo)
            Console.WriteLine(miResolved)
            Console.WriteLine("Is the resolved method the same? {0}", miResolved Is 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 = CType(m.ResolveMethod(CInt(Tokens.Case5)), MethodInfo)
    
        End Sub 
    End Class 
End Namespace

' 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
' 

설명

시그니처에 요소 형식 ELEMENT_TYPE_VAR 또는 ELEMENT_TYPE_MVAR포함 된 MethodSpec에 대 한 메타 데이터 토큰을 확인 하려면 필요한 컨텍스트를 제공할 수 있는 ResolveMethod(Int32, Type[], Type[]) 메서드 오버 로드를 사용 합니다.To resolve a metadata token for a MethodSpec whose signature contains element type ELEMENT_TYPE_VAR or ELEMENT_TYPE_MVAR, use the ResolveMethod(Int32, Type[], Type[]) method overload, which allows you to supply the necessary context. 즉, 제네릭 형식의 제네릭 형식 매개 변수에 따라 달라 지는 메서드 및/또는 토큰이 포함 된 제네릭 메서드에 대 한 메타 데이터 토큰을 확인 하는 경우 해당 형식 매개 변수를 제공할 수 있는 오버 로드를 사용 해야 합니다.That is, when you are resolving a metadata token for a method that depends on the generic type parameters of the generic type and/or the generic method in which the token is embedded, you must use the overload that allows you to supply those type parameters.

참고

공용 언어 인프라 (CLI) 설명서에서 "II: 메타 데이터 정의 및 의미" 메타 데이터 토큰에 대 한 정보를 찾을 수 있습니다.Information about metadata tokens can be found in the Common Language Infrastructure (CLI) documentation, especially "Partition II: Metadata Definition and Semantics". 설명서는 온라인으로 제공 됩니다. ECMA 웹 사이트의 MSDN 및 표준 ecma-335-CLI (공용 언어 인프라) 에서 Ecma C# 및 공용 언어 인프라 표준 을 참조 하세요.The documentation is available online; see ECMA C# and Common Language Infrastructure Standards on MSDN and Standard ECMA-335 - Common Language Infrastructure (CLI) on the ECMA Web site.

적용 대상