Module.ResolveMethod Method (Int32, Type[], Type[])

Returns the method or constructor identified by the specified metadata token, in the context defined by the specified generic type parameters.

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

public MethodBase ResolveMethod (
	int metadataToken,
	Type[] genericTypeArguments,
	Type[] genericMethodArguments
)
public MethodBase ResolveMethod (
	int metadataToken, 
	Type[] genericTypeArguments, 
	Type[] genericMethodArguments
)
public function ResolveMethod (
	metadataToken : int, 
	genericTypeArguments : Type[], 
	genericMethodArguments : Type[]
) : MethodBase
Not applicable.

Parameters

metadataToken

A metadata token that identifies a method or constructor in the module.

genericTypeArguments

An array of Type objects representing the generic type arguments of the type where the token is in scope, or a null reference (Nothing in Visual Basic) if that type is not generic.

genericMethodArguments

An array of Type objects representing the generic type arguments of the method where the token is in scope, or a null reference (Nothing in Visual Basic) if that method is not generic.

Return Value

A MethodBase object representing the method that is identified by the specified metadata token.

Exception typeCondition

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), and the necessary generic type arguments were not supplied for either or both of genericTypeArguments and genericMethodArguments.

ArgumentOutOfRangeException

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

Use the Type.GetGenericArguments method on the type where metadataToken is in scope to obtain an array of generic type arguments for genericTypeArguments. Use the MethodInfo.GetGenericArguments method on the method where metadataToken is in scope to obtain an array of generic type arguments for genericMethodArguments. It is always safe to provide these arguments, even when they are not needed.

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.

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

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

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Show: