Module::ResolveMethod Method (Int32, array<Type^>^, array<Type^>^)
Returns the method or constructor identified by the specified metadata token, in the context defined by the specified generic type parameters.
Assembly: mscorlib (in mscorlib.dll)
public: virtual MethodBase^ ResolveMethod( int metadataToken, array<Type^>^ genericTypeArguments, array<Type^>^ genericMethodArguments )
Parameters
- metadataToken
-
Type:
System::Int32
A metadata token that identifies a method or constructor in the module.
- genericTypeArguments
-
Type:
array<System::Type^>^
An array of Type objects representing the generic type arguments of the type where the token is in scope, or null if that type is not generic.
- genericMethodArguments
-
Type:
array<System::Type^>^
An array of Type objects representing the generic type arguments of the method where the token is in scope, or null if that method is not generic.
Return Value
Type: System.Reflection::MethodBase^A MethodBase object representing the method that is identified by the specified metadata token.
| 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), 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.
Note |
|---|
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, array<Type^>^, array<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.
Available since 2.0
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
