Module.ResolveMethod Methode

Definition

Gibt die von einem Metadatentoken angegebene Methode zurück.

Überlädt

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

Gibt die vom angegebenen Metadatentoken angegebene Methode oder den vom angegebenen Metadatentoken angegebenen Konstruktor im von den angegebenen generischen Typparametern definierten Kontext zurück.

ResolveMethod(Int32)

Gibt die Methode oder den Konstruktor zurück, die bzw. der vom angegebenen Metadatentoken angegeben wird.

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

Quelle:
Module.cs
Quelle:
Module.cs
Quelle:
Module.cs

Gibt die vom angegebenen Metadatentoken angegebene Methode oder den vom angegebenen Metadatentoken angegebenen Konstruktor im von den angegebenen generischen Typparametern definierten Kontext zurück.

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

Parameter

metadataToken
Int32

Ein Metadatentoken, das eine Methode oder einen Konstruktor im Modul identifiziert.

genericTypeArguments
Type[]

Ein Array von Type-Objekten, die die generischen Typargumente des Typs darstellen, in dessen Gültigkeitsbereich sich das Token befindet, oder null, wenn dieser Typ nicht generisch ist.

genericMethodArguments
Type[]

Ein Array von Type-Objekten, die die generischen Typargumente der Methode darstellen, in deren Gültigkeitsbereich sich das Token befindet, oder null, wenn diese Methode nicht generisch ist.

Gibt zurück

Ein MethodBase-Objekt, das die Methode darstellt, die durch das angegebene Metadatentoken angegeben wird.

Ausnahmen

metadataToken ist im Bereich des aktuellen Moduls kein Token für eine Methode oder einen Konstruktor.

- oder -

metadataToken ist eine MethodSpec, deren Signatur Elementtyp var (Typparameter eines generischen Typs) oder mvar (Typparameter einer generischen Methode) enthält, und die erforderlichen Argumente des generischen Typs wurden für genericTypeArguments und/oder genericMethodArguments nicht angegeben.

metadataToken ist im Bereich des aktuellen Moduls kein gültiges Token.

Beispiele

Das folgende Beispiel zeigt, wie die beiden Überladungen der ResolveMethod -Methode verwendet werden, um Metadatentoken von Aufrufwebsites in generischen und nicht generischen Kontexten aufzulösen.

Im Codebeispiel werden zwei generische Typen definiert, G1<Tg1> und G2<Tg2>jeder von ihnen verfügt über eine generische Methode. G1<Tg1> verfügt außerdem über eine nicht generische Methode, die den Typparameter Tg1 für den Parameter verwendet. Die generische Methode GM2<Tgm2> im Typ G2<Tg2> enthält mehrere Methodenaufrufe:

  • Fall 1: Die generische Methode GM1<Tgm1> wird aufgerufen, wobei die Typparameter von G2<Tg2> und GM2<Tgm2> als Typargumente verwendet werden. Anders ausgedrückt: Die Parametertypen der aufgerufenen Methode hängen von den Typen ab, die zum Erstellen eines geschlossenen generischen Typs aus der Typdefinition für G2<Tg2>verwendet werden.

  • Fall 2: Die nicht generische Methode M1 wird aufgerufen. Der Parameter dieser Methode verwendet den Typparameter des definierenden Typs , G1<Tg1>der in diesem Fall durch den Typparameter des einschließenden Typs G2<Tg2>ersetzt wird.

  • Fall 3: Die generische Methode GM1<Tgm1> aufgerufen wird, wird die Angabe Int32 und Object für die Typargumente eines generischen Typs und der generischen Methode bzw. Dieser Methodenaufruf hängt nicht von den Typparametern des einschließenden Typs oder der einschließenden Methode ab.

  • Fall 4: Die nicht generische Methode M1 der Example -Klasse wird aufgerufen. Dieser Methodenaufruf hängt nicht von den Typparametern des einschließenden Typs oder der einschließenden Methode ab.

Darüber hinaus definiert das Beispiel die nicht generische Example Klasse. Diese Klasse verfügt über eine Methode M , die eine generische Methode aufruft.

  • Fall 5: Die generische Methode GM1<Tgm1> aufgerufen wird, wird die Angabe Int32 und Object für die Typargumente eines generischen Typs und der generischen Methode bzw. Der Kontext für diese Methode enthält keinen generischen Typ oder keine generische Methode.

Für jeden Fall erstellt das Beispiel zunächst eine MethodInfo , die die aufgerufene Methode darstellt, und löst dann das Token mithilfe der ResolveMethod(Int32, Type[], Type[]) Methodenüberladung auf, wobei die Type.GetGenericArguments Methoden und die Methoden verwendet MethodInfo.GetGenericArguments werden, um die Werte für die genericTypeArguments Parameter und genericMethodArguments abzurufen. Diese Technik funktioniert in allen Fällen, da die Methoden für nicht generische Kontexte zurückgeben Type.EmptyTypes . Im Beispiel wird das aufgelöste MethodInfo mit dem konstruierten MethodInfoverglichen.

Im Beispiel wird dann versucht, die ResolveMethod(Int32) Methodenüberladung zu verwenden, um das Token aufzulösen. Dies funktioniert in den Fällen 3, 4 und 5, da die Methodenaufrufe nicht vom generischen Kontext abhängen. In den Fällen 1 und 2 wird eine Ausnahme ausgelöst, da nicht genügend Informationen zum Auflösen des Tokens vorhanden sind.

Die Metadatentokenwerte werden als Enumeration hartcodiert. Wenn Sie dieses Codebeispiel ändern, ändern sich wahrscheinlich die Tokenwerte. Um die neuen Tokenwerte zu ermitteln, kompilieren Sie den Code, und verwenden Sie Ildasm.exe mit der Option /TOKENS , um die Assembly zu untersuchen. Die Token finden Sie an den Aufrufpunkten. Fügen Sie die neuen Werte in die -Enumeration ein, und kompilieren Sie das Beispiel erneut.

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
'

Hinweise

Verwenden Sie die Type.GetGenericArguments -Methode für den Typ, bei dem metadataToken sich im Bereich befindet, um ein Array von generischen Typargumenten für genericTypeArgumentsabzurufen. Verwenden Sie die MethodInfo.GetGenericArguments -Methode für die -Methode, in der metadataToken sich im Bereich befindet, um ein Array von generischen Typargumenten für genericMethodArgumentsabzurufen. Es ist immer sicher, diese Argumente bereitzustellen, auch wenn sie nicht benötigt werden.

Hinweis

Informationen zu Metadatentoken finden Sie in der Dokumentation zur Common Language Infrastructure (CLI), insbesondere "Partition II: Metadatendefinition und -semantik". Weitere Informationen finden Sie unter ECMA 335 Common Language Infrastructure (CLI).

Gilt für:

ResolveMethod(Int32)

Quelle:
Module.cs
Quelle:
Module.cs
Quelle:
Module.cs

Gibt die Methode oder den Konstruktor zurück, die bzw. der vom angegebenen Metadatentoken angegeben wird.

public:
 System::Reflection::MethodBase ^ ResolveMethod(int metadataToken);
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

Parameter

metadataToken
Int32

Ein Metadatentoken, das eine Methode oder einen Konstruktor im Modul identifiziert.

Gibt zurück

Ein MethodBase-Objekt, das die Methode oder den Konstruktor darstellt, die bzw. der vom angegebenen Metadatentoken angegeben wird.

Ausnahmen

metadataToken ist im Bereich des aktuellen Moduls kein Token für eine Methode oder einen Konstruktor.

- oder -

metadataToken ist eine MethodSpec, deren Signatur Elementtyp var (Typparameter eines generischen Typs) oder mvar (Typparameter einer generischen Methode) enthält.

metadataToken ist im Bereich des aktuellen Moduls kein gültiges Token.

Beispiele

Das folgende Beispiel zeigt, wie die beiden Überladungen der ResolveMethod -Methode verwendet werden, um Metadatentoken von Aufrufwebsites in generischen und nicht generischen Kontexten aufzulösen.

Im Codebeispiel werden zwei generische Typen definiert, G1<Tg1> und G2<Tg2> (G1(Of Tg1) und G2(Of Tg2) in Visual Basic), von denen jeder über eine generische Methode verfügt. G1<Tg1> verfügt außerdem über eine nicht generische Methode, die den Typparameter Tg1 für den Parameter verwendet. Die generische Methode GM2<Tgm2> im Typ G2<Tg2> enthält mehrere Methodenaufrufe:

  • Fall 1: Die generische Methode GM1<Tgm1> wird aufgerufen, wobei die Typparameter von G2<Tg2> und GM2<Tgm2> als Typargumente verwendet werden. Anders ausgedrückt: Die Parametertypen der aufgerufenen Methode hängen von den Typen ab, die zum Erstellen eines geschlossenen generischen Typs aus der Typdefinition für G2<Tg2>verwendet werden.

  • Fall 2: Die nicht generische Methode M1 wird aufgerufen. Der Parameter dieser Methode verwendet den Typparameter des definierenden Typs , G1<Tg1>der in diesem Fall durch den Typparameter des einschließenden Typs G2<Tg2>ersetzt wird.

  • Fall 3: Die generische Methode GM1<Tgm1> aufgerufen wird, wird die Angabe Int32 und Object für die Typargumente eines generischen Typs und der generischen Methode bzw. Dieser Methodenaufruf hängt nicht von den Typparametern des einschließenden Typs oder der einschließenden Methode ab.

  • Fall 4: Die nicht generische Methode M1 der Example -Klasse wird aufgerufen. Dieser Methodenaufruf hängt nicht von den Typparametern des einschließenden Typs oder der einschließenden Methode ab.

Darüber hinaus definiert das Beispiel die nicht generische Example Klasse. Diese Klasse verfügt über eine Methode M , die eine generische Methode aufruft:

  • Fall 5: Die generische Methode GM1 aufgerufen wird, wird die Angabe Int32 und Object für die Typargumente eines generischen Typs und der generischen Methode bzw. Der Kontext für diese Methode enthält keinen generischen Typ oder keine generische Methode.

Für jeden Fall erstellt das Beispiel zunächst eine MethodInfo , die die aufgerufene Methode darstellt, und löst dann das Token mithilfe der ResolveMethod(Int32, Type[], Type[]) Methodenüberladung auf, wobei die Type.GetGenericArguments Methoden und die Methoden verwendet MethodInfo.GetGenericArguments werden, um die Werte für die genericTypeArguments Parameter und genericMethodArguments abzurufen. Diese Technik funktioniert in allen Fällen, da die Methoden für nicht generische Kontexte zurückgeben Type.EmptyTypes . Im Beispiel wird das aufgelöste MethodInfo mit dem konstruierten MethodInfoverglichen.

Im Beispiel wird dann versucht, die ResolveMethod(Int32) Methodenüberladung zu verwenden, um das Token aufzulösen. Dies funktioniert in den Fällen 3, 4 und 5, da die Methodenaufrufe nicht vom generischen Kontext abhängen. In den Fällen 1 und 2 wird eine Ausnahme ausgelöst, da nicht genügend Informationen zum Auflösen des Tokens vorhanden sind.

Die Metadatentokenwerte werden als Enumeration hartcodiert. Wenn Sie dieses Codebeispiel ändern, ändern sich wahrscheinlich die Tokenwerte. Um die neuen Tokenwerte zu ermitteln, kompilieren Sie den Code, und verwenden Sie Ildasm.exe mit der Option /TOKENS , um die Assembly zu untersuchen. Die Token finden Sie an den Aufrufpunkten. Fügen Sie die neuen Werte in die -Enumeration ein, und kompilieren Sie das Beispiel erneut.

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
'

Hinweise

Verwenden Sie die Methodenüberladung, mit der ResolveMethod(Int32, Type[], Type[]) Sie den erforderlichen Kontext angeben können, um ein Metadatentoken für ein MethodSpec aufzulösen, dessen Signatur den Elementtyp ELEMENT_TYPE_VAR oder ELEMENT_TYPE_MVARenthält. Das heißt, wenn Sie ein Metadatentoken für eine Methode auflösen, die von den generischen Typparametern des generischen Typs und/oder der generischen Methode abhängt, in die das Token eingebettet ist, müssen Sie die Überladung verwenden, mit der Sie diese Typparameter angeben können.

Hinweis

Informationen zu Metadatentoken finden Sie in der Dokumentation zur Common Language Infrastructure (CLI), insbesondere "Partition II: Metadatendefinition und -semantik". Weitere Informationen finden Sie unter ECMA 335 Common Language Infrastructure (CLI).

Gilt für: