Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ModuleBuilder.GetArrayMethod Method

Returns the named method on an array class.

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

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.

Exceptions

Exception Type Condition
ArgumentException arrayClass is not an array.
ArgumentNullException arrayClass or methodName is a null reference (Nothing in Visual Basic).

Remarks

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.

Example

[Visual Basic, C#, C++] The following code sample demonstrates how to obtain the MethodInfo 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:
© 2015 Microsoft