Export (0) Print
Expand All

ModuleBuilder.GetArrayMethodToken Method

Returns the token for the named method on an array class.

[Visual Basic]
Public Function GetArrayMethodToken( _
   ByVal arrayClass As Type, _
   ByVal methodName As String, _
   ByVal callingConvention As CallingConventions, _
   ByVal returnType As Type, _
   ByVal parameterTypes() As Type _
) As MethodToken
[C#]
public MethodToken GetArrayMethodToken(
 Type arrayClass,
 string methodName,
 CallingConventions callingConvention,
 Type returnType,
 Type[] parameterTypes
);
[C++]
public: MethodToken GetArrayMethodToken(
 Type* arrayClass,
 String* methodName,
 CallingConventions callingConvention,
 Type* returnType,
 Type* parameterTypes[]
);
[JScript]
public function GetArrayMethodToken(
   arrayClass : Type,
 methodName : String,
 callingConvention : CallingConventions,
 returnType : Type,
 parameterTypes : Type[]
) : MethodToken;

Parameters

arrayClass
The Type object for the array.
methodName
A string containing the name of the method.
callingConvention
The calling convention for the method.
returnType
The return type of the method.
parameterTypes
The types of the parameters of the method.

Return Value

The token for the named method on an array class.

Exceptions

Exception Type Condition
ArgumentException arrayClass is not an array.

-or-

Length of methodName is zero.

ArgumentNullException arrayClass or methodName is a null reference (Nothing in Visual Basic).

Remarks

This method is similar to GetArrayMethod, except that it returns the token of the array method instead of the method itself.

Example

[Visual Basic, C#, C++] The following code sample demonstrates how to obtain the MethodToken corresponding to a method which returns an array value, using GetArrayMethod.

[Visual Basic] 
' Define a dynamic module in "TempAssembly" assembly.
Dim myModuleBuilder As ModuleBuilder = myAssemblyBuilder.DefineDynamicModule("TempModule")
' Define a runtime class with specified name and attributes.
Dim myTypeBuilder As TypeBuilder = _
            myModuleBuilder.DefineType("TempClass", TypeAttributes.Public)
Dim myParamArray() As Type = New Type() {GetType(Array)}
' Add 'SortArray' method to the class, with the given signature.
Dim myMethod As MethodBuilder = _
   myTypeBuilder.DefineMethod("SortArray", MethodAttributes.Public, _
   GetType(Array), myParamArray)

Dim myArrayClass(0) As Type
Dim parameterTypes() As Type = New Type() {GetType(Array)}
' Get the 'MethodInfo' object corresponding to 'Sort' method of 'Array' class.
Dim myMethodInfo As MethodInfo = _
   myModuleBuilder.GetArrayMethod(myArrayClass.GetType(), "Sort", _
   CallingConventions.Standard, Nothing, parameterTypes)
' Get the token corresponding to 'Sort' method of 'Array' class.
Dim myMethodToken As MethodToken = _
      myModuleBuilder.GetArrayMethodToken(myArrayClass.GetType(), _
      "Sort", CallingConventions.Standard, Nothing, parameterTypes)
Console.WriteLine("Token used by module to identify the 'Sort' method" + _
                  " of 'Array' class is : {0:x} ", myMethodToken.Token)
Dim methodIL As ILGenerator = myMethod.GetILGenerator()
methodIL.Emit(OpCodes.Ldarg_1)
methodIL.Emit(OpCodes.Call, myMethodInfo)
methodIL.Emit(OpCodes.Ldarg_1)
methodIL.Emit(OpCodes.Ret)
' Complete the creation of type.
myTypeBuilder.CreateType()

[C#] 
// Define a dynamic module in "TempAssembly" assembly.
ModuleBuilder myModuleBuilder = myAssemblyBuilder.
                              DefineDynamicModule("TempModule");
// Define a runtime class with specified name and attributes.
TypeBuilder myTypeBuilder = myModuleBuilder.DefineType
                           ("TempClass",TypeAttributes.Public);
Type[] paramArray = {typeof(Array)};
// Add 'SortArray' method to the class, with the given signature.
MethodBuilder myMethod = myTypeBuilder.DefineMethod("SortArray", 
                          MethodAttributes.Public,typeof(Array),paramArray);

Type[] myArrayClass = new Type[1];
Type[] parameterTypes = {typeof(Array)};
// Get the 'MethodInfo' object corresponding to 'Sort' method of 'Array' class.
MethodInfo myMethodInfo=myModuleBuilder.GetArrayMethod(
            myArrayClass.GetType(),"Sort",CallingConventions.Standard,
                                                                         null,parameterTypes);
// Get the token corresponding to 'Sort' method of 'Array' class.
MethodToken myMethodToken=myModuleBuilder.GetArrayMethodToken(
            myArrayClass.GetType(),"Sort",CallingConventions.Standard,
                                                                        null,parameterTypes);
Console.WriteLine("Token used by module to identify the 'Sort' method"
                            + " of 'Array' class is : {0:x} ",myMethodToken.Token);

ILGenerator methodIL = myMethod.GetILGenerator();
methodIL.Emit(OpCodes.Ldarg_1);
methodIL.Emit(OpCodes.Call,myMethodInfo);
methodIL.Emit(OpCodes.Ldarg_1);
methodIL.Emit(OpCodes.Ret);

// Complete the creation of type.
myTypeBuilder.CreateType();

[C++] 
// Define a dynamic module in "TempAssembly" assembly.
ModuleBuilder* myModuleBuilder = myAssemblyBuilder->
   DefineDynamicModule(S"TempModule");
// Define a runtime class with specified name and attributes.
TypeBuilder* myTypeBuilder = myModuleBuilder->DefineType
   (S"TempClass",TypeAttributes::Public);
Type* paramArray[] = {__typeof(Array)};
// Add 'SortArray' method to the class, with the given signature.
MethodBuilder* myMethod = myTypeBuilder->DefineMethod(S"SortArray", 
   MethodAttributes::Public,__typeof(Array),paramArray);

Type* myArrayClass[] = new Type*[1];
Type* parameterTypes[] = {__typeof(Array)};
// Get the 'MethodInfo' object corresponding to 'Sort' method of 'Array' class.
MethodInfo* myMethodInfo=myModuleBuilder->GetArrayMethod(
   myArrayClass->GetType(),S"Sort",CallingConventions::Standard,
   0,parameterTypes);
// Get the token corresponding to 'Sort' method of 'Array' class.
MethodToken myMethodToken=myModuleBuilder->GetArrayMethodToken(
   myArrayClass->GetType(),S"Sort",CallingConventions::Standard,
   0,parameterTypes);
Console::WriteLine(S"Token used by module to identify the 'Sort' method"
   S" of 'Array' class is : {0:x} ", __box(myMethodToken.Token));

ILGenerator* methodIL = myMethod->GetILGenerator();
methodIL->Emit(OpCodes::Ldarg_1);
methodIL->Emit(OpCodes::Call,myMethodInfo);
methodIL->Emit(OpCodes::Ldarg_1);
methodIL->Emit(OpCodes::Ret);

// Complete the creation of type.
myTypeBuilder->CreateType();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ModuleBuilder Class | ModuleBuilder Members | System.Reflection.Emit Namespace

Show:
© 2014 Microsoft