Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ModuleBuilder.GetArrayMethod Method

Returns the named method on an array class.

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

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

Parameters

arrayClass

An array class.

methodName

The name of a method on the array class.

callingConvention

The method's calling convention.

returnType

The return type of the method.

parameterTypes

The types of the method's parameters.

Return Value

The named method on an array class.

Exception typeCondition

ArgumentException

arrayClass is not an array.

ArgumentNullException

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

GetArrayMethod is useful when you have an array of a type whose definition has not been completed and you want to access methods defined on Array. For example, you might define a type and want to define a method that takes an array of the type as a parameter. In order to access the elements of the array, you will need to call methods of the Array class.

The following code sample demonstrates how to obtain the MethodInfo 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
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft