Module.ResolveMethod Module.ResolveMethod Module.ResolveMethod Module.ResolveMethod Method

定义

返回由元数据令牌标识的方法。Returns the method identified by a metadata token.

重载

ResolveMethod(Int32, Type[], Type[]) ResolveMethod(Int32, Type[], Type[]) ResolveMethod(Int32, Type[], Type[]) 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) ResolveMethod(Int32) ResolveMethod(Int32) ResolveMethod(Int32)

返回由指定的元数据令牌标识的方法或构造函数。Returns the method or constructor identified by the specified metadata token.

ResolveMethod(Int32, Type[], Type[]) ResolveMethod(Int32, Type[], Type[]) ResolveMethod(Int32, Type[], Type[]) 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 Int32 Int32 Int32

一个元数据令牌,用于标识模块中的方法或构造函数。A metadata token that identifies a method or constructor in the module.

genericTypeArguments
Type[]

一个 Type 对象的数组,表示以下类型的泛型类型参数,在该类型中,令牌在范围内;如果该类型不是泛型类型,则为 nullAn 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 对象的数组,表示以下方法的泛型类型参数,在该方法中,令牌在范围内;如果该方法不是泛型方法,则为 nullAn 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- metadataToken 是一个 MethodSpec,其签名包含元素类型 var(泛型类型的类型参数)或 mvar(泛型方法的类型参数),并且没有为 genericTypeArguments 和(或)genericMethodArguments 提供必要的泛型类型参数。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. GM2<Tgm2> 类型G2<Tg2>中的泛型方法包含若干方法调用:The generic method GM2<Tgm2> in type G2<Tg2> contains several method calls:

  • 案例1:调用泛型方法GM1<Tgm1>G2<Tg2>GM2<Tgm2>使用的类型参数作为类型参数。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:调用非泛型方法M1Case 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> ,并分别为Int32泛型Object类型和泛型方法的类型参数指定和。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> ,并分别为Int32泛型Object类型和泛型方法的类型参数指定和。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一个表示所调用方法的,然后ResolveMethod(Int32, Type[], Type[])使用Type.GetGenericArguments方法重载解析该标记,方法是使用genericTypeArgumentsMethodInfo.GetGenericArguments方法获取和的值genericMethodArguments参数。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中,引发异常,因为没有足够的信息来解析令牌。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选项来检查程序集。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
' 

注解

在类型中对类型使用metadataToken genericTypeArguments方法,其中在范围内可获取的泛型类型参数的数组。 Type.GetGenericArgumentsUse the Type.GetGenericArguments method on the type where metadataToken is in scope to obtain an array of generic type arguments for genericTypeArguments. 对方法使用metadataToken genericMethodArguments方法,其中在范围内可获取的泛型类型参数的数组。 MethodInfo.GetGenericArgumentsUse 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)文档,尤其是 "第二部分:元数据定义和语义 "。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) ResolveMethod(Int32) ResolveMethod(Int32) 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 Int32 Int32 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- metadataToken 是一个 MethodSpec,其签名包含元素类型 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>G1(Of Tg1)G2(Of Tg2)定义了(在 Visual Basic 中),其中每个类型都有一个泛型方法。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. GM2<Tgm2> 类型G2<Tg2>中的泛型方法包含若干方法调用:The generic method GM2<Tgm2> in type G2<Tg2> contains several method calls:

  • 案例1:调用泛型方法GM1<Tgm1>G2<Tg2>GM2<Tgm2>使用的类型参数作为类型参数。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:调用非泛型方法M1Case 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> ,并分别为Int32泛型Object类型和泛型方法的类型参数指定和。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 ,并分别为Int32泛型Object类型和泛型方法的类型参数指定和。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一个表示所调用方法的,然后ResolveMethod(Int32, Type[], Type[])使用Type.GetGenericArguments方法重载解析该标记,方法是使用genericTypeArgumentsMethodInfo.GetGenericArguments方法获取和的值genericMethodArguments参数。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中,引发异常,因为没有足够的信息来解析令牌。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选项来检查程序集。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
' 

注解

若要解析其签名包含元素MethodSpec类型ELEMENT_TYPE_VARELEMENT_TYPE_MVAR的元数据标记,请使用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)文档,尤其是 "第二部分:元数据定义和语义 "。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.

适用于