GetArrayMethodToken Method

ModuleBuilder.GetArrayMethodToken Method

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

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

public MethodToken GetArrayMethodToken (
	Type arrayClass,
	string methodName,
	CallingConventions callingConvention,
	Type returnType,
	Type[] parameterTypes
)
public MethodToken GetArrayMethodToken (
	Type arrayClass, 
	String methodName, 
	CallingConventions callingConvention, 
	Type returnType, 
	Type[] parameterTypes
)
public function GetArrayMethodToken (
	arrayClass : Type, 
	methodName : String, 
	callingConvention : CallingConventions, 
	returnType : Type, 
	parameterTypes : Type[]
) : MethodToken
Not applicable.

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.

Exception typeCondition

ArgumentException

arrayClass is not an array.

-or-

Length of methodName is zero.

ArgumentNullException

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

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

The following code sample demonstrates how to obtain the MethodToken corresponding to a method which returns an array value, using GetArrayMethod.

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

// 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[] = { Array.class.ToType() };

// Add 'SortArray' method to the class, with the given signature.
MethodBuilder myMethod = myTypeBuilder.DefineMethod("SortArray", 
    MethodAttributes.Public, Array.class.ToType(), paramArray);

Type myArrayClass[] = new Type[1];
Type parameterTypes[] = { Array.class.ToType() };

// 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} ", (Int32)myMethodToken.get_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();

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, 1.1, 1.0

Community Additions

ADD
Show:
© 2016 Microsoft