Bagikan melalui


Module.ResolveMethod Method (Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the method or constructor that is identified by the specified metadata token.

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Function ResolveMethod ( _
    metadataToken As Integer _
) As MethodBase
public MethodBase ResolveMethod(
    int metadataToken
)

Parameters

  • metadataToken
    Type: System.Int32
    A metadata token that identifies a method or constructor in the module.

Return Value

Type: System.Reflection.MethodBase
The method or constructor that is identified by the specified metadata token.

Exceptions

Exception Condition
ArgumentException

metadataToken is not a token for a method or constructor in the scope of the current module.

-or-

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).

ArgumentOutOfRangeException

metadataToken is not a valid token in the scope of the current module.

Remarks

To resolve a metadata token for a MethodSpec whose signature contains element type ELEMENT_TYPE_VAR or ELEMENT_TYPE_MVAR, use the ResolveMethod(Int32, array<Type[], array<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.

NoteNote:

Information about metadata tokens can be found in the Common Language Infrastructure (CLI) documentation, especially "Partition II: Metadata Definition and Semantics". 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.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 ResolveMethod does not throw an ArgumentOutOfRangeException exception if you pass an invalid parameter value. Instead, it throws an ArgumentException exception.

Examples

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.

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> also has a non-generic method that uses the type parameter Tg1 for its parameter. The generic method GM2<Tgm2> in type G2<Tg2> contains several method calls:

  • Case 1: The generic method GM1<Tgm1> is called, using the type parameters of G2<Tg2> and GM2<Tgm2> as the type arguments. 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>.

  • Case 2: The non-generic method M1 is called. 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>.

  • 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.

  • 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.

In addition, the example defines the non-generic Example class. This class has a method M that makes a call to a generic method:

  • 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.

For each case, the example first constructs a MethodInfo that represents the called method, and then resolves the token using the ResolveMethod(Int32, array<Type[], array<Type[]) method overload, and using the Type.GetGenericArguments and MethodInfo.GetGenericArguments methods to get the values for the genericTypeArguments and genericMethodArguments parameters. This technique works in all cases, because the methods return Type.EmptyTypes for non-generic contexts. The example compares the resolved MethodInfo with the constructed MethodInfo.

The example then attempts to use the ResolveMethod(Int32) method overload to resolve the token. This works in cases 3, 4, and 5, because the method calls do not depend on the generic context. 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. 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.

Imports System.Reflection

' 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. Note: Only the
' low-order bits will change! The high-order bits for eac case (for example,
' 2b000000 in Case1) are determined by metadata conventions.
Enum Tokens
   Case1 = &H2B000001
   Case2 = &HA000010
   Case3 = &H2B000002
   Case4 = &H600000C
   Case5 = &H2B000002
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

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Try
         Dim m As [Module] = [Assembly].GetExecutingAssembly().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())
         outputBlock.Text &= vbCrLf & "Case 1:" & vbCrLf & miTest.ToString() & vbCrLf

         ' 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)
         outputBlock.Text &= miResolved.ToString() & vbCrLf
         outputBlock.Text += String.Format("Is the resolved method the same? {0}", miResolved Is miTest) & vbCrLf

         ' 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
            outputBlock.Text += String.Format("{0}: {1}", ex.GetType(), ex.Message) & vbCrLf
         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")
         outputBlock.Text &= vbCrLf & "Case 2:" & vbCrLf & miTest.ToString() & vbCrLf

         ' 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)
         outputBlock.Text &= miResolved.ToString() & vbCrLf
         outputBlock.Text += String.Format("Is the resolved method the same? {0}", miResolved Is miTest) & vbCrLf

         ' 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
            outputBlock.Text += String.Format("{0}: {1}", ex.GetType(), ex.Message) & vbCrLf
         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)})
         outputBlock.Text &= vbCrLf & "Case 3:" & vbCrLf & miTest.ToString() & vbCrLf

         ' 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)
         outputBlock.Text &= miResolved.ToString() & vbCrLf
         outputBlock.Text += String.Format("Is the resolved method the same? {0}", miResolved Is miTest) & vbCrLf

         ' 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")
         outputBlock.Text &= vbCrLf & "Case 4:" & vbCrLf & miTest.ToString() & vbCrLf

         ' 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)
         outputBlock.Text &= miResolved.ToString() & vbCrLf
         outputBlock.Text += String.Format("Is the resolved method the same? {0}", miResolved Is miTest) & vbCrLf

         ' 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)})
         outputBlock.Text &= vbCrLf & "Case 5:" & vbCrLf & miTest.ToString() & vbCrLf

         ' 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)
         outputBlock.Text &= miResolved.ToString() & vbCrLf
         outputBlock.Text += String.Format("Is the resolved method the same? {0}", miResolved Is miTest) & vbCrLf

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

         outputBlock.Text &= vbLf & vbLf & _
            "Note: If False appears after ""Is the resolved method the same?"" for any case," & vbLf & _
            "the corresponding token in the Tokens enumeration is wrong. Use Ildasm.exe to " & vbLf & _
            "determine the correct token value. The tokens you need are at the ends of the" & vbLf & _
            "callvirt lines in G2.GM2() and Example.M()."

      Catch ex As Exception
         outputBlock.Text &= _
            "--------------------" & vbLf & _
            "An unhandled exception occurred in Example.Demo, because a token in the Tokens" & vbLf & _
            "enumeration is wrong. Use Ildasm.exe to determine the correct token value. The" & vbLf & _
            "The tokens you need are at the ends of the callvirt lines in G2.GM2() and " & vbLf & _
            "Example.M()." & vblf
      End Try
   End Sub
End Class

' 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
' 
'
'Note: If False appears after ""Is the resolved method the same?"" for any case," & vbLf & _
'the corresponding token in the Tokens enumeration is wrong. Use Ildasm.exe to " & vbLf & _
'determine the correct token value. The tokens you need are at the ends of the" & vbLf & _
'callvirt lines in G2.GM2() and Example.M()."
using System;
using System.Reflection;

// 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. Note: Only the
// low-order bits will change! The high-order bits for eac case (for example,
// 2b000000 in Case1) are determined by metadata conventions.
enum Tokens
{
   Case1 = 0x2b000001,
   Case2 = 0x0A000014,
   Case3 = 0x2b000002,
   Case4 = 0x0600000D,
   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());
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      try
      {
         Module mod = Assembly.GetExecutingAssembly().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());
         outputBlock.Text += String.Format("\nCase 1:\n{0}", miTest) + "\n";

         // 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());
         outputBlock.Text += miResolved + "\n";
         outputBlock.Text += String.Format("Is the resolved method the same? {0}", miResolved == miTest) + "\n";

         // 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)
         {
            outputBlock.Text += String.Format("{0}: {1}", ex.GetType(), ex.Message) + "\n";
         }


         // 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");
         outputBlock.Text += String.Format("\nCase 2:\n{0}", miTest) + "\n";

         // 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());
         outputBlock.Text += miResolved + "\n";
         outputBlock.Text += String.Format("Is the resolved method the same? {0}", miResolved == miTest) + "\n";

         // 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)
         {
            outputBlock.Text += String.Format("{0}: {1}", ex.GetType(), ex.Message) + "\n";
         }


         // 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) });
         outputBlock.Text += String.Format("\nCase 3:\n{0}", miTest) + "\n";

         // 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());
         outputBlock.Text += miResolved + "\n";
         outputBlock.Text += String.Format("Is the resolved method the same? {0}", miResolved == miTest) + "\n";

         // 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");
         outputBlock.Text += String.Format("\nCase 4:\n{0}", miTest) + "\n";

         // 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());
         outputBlock.Text += miResolved + "\n";
         outputBlock.Text += String.Format("Is the resolved method the same? {0}", miResolved == miTest) + "\n";

         // 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) });
         outputBlock.Text += String.Format("\nCase 5:\n{0}", miTest) + "\n";

         // 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());
         outputBlock.Text += miResolved + "\n";
         outputBlock.Text += String.Format("Is the resolved method the same? {0}", miResolved == miTest) + "\n";

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

         outputBlock.Text += 
            "\n\nNote: If False appears after \"Is the resolved method the same?\" for any case," +
            "\nthe corresponding token in the Tokens enumeration is wrong. Use Ildasm.exe to " +
            "\ndetermine the correct token value. The tokens you need are at the ends of the" +
            "\ncallvirt lines in G2.GM2() and Example.M().\n";

      }
      catch (Exception ex)
      {
         outputBlock.Text += 
            "--------------------" +
            "\nAn unhandled exception occurred in Example.Demo, because a token in the Tokens" +
            "\nenumeration is wrong. Use Ildasm.exe to determine the correct token value. The" +
            "\nThe tokens you need are at the ends of the callvirt lines in G2.GM2() and " +
            "\nExample.M().\n";
      }
   }
}
/* 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


Note: If False appears after ""Is the resolved method the same?"" for any case," & vbLf & _
the corresponding token in the Tokens enumeration is wrong. Use Ildasm.exe to " & vbLf & _
determine the correct token value. The tokens you need are at the ends of the" & vbLf & _
callvirt lines in G2.GM2() and Example.M()."
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.