Export (0) Print
Expand All

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


Type: System.Type
An array class.
Type: System.String
The name of a method on the array class.
Type: System.Reflection.CallingConventions
The method's calling convention.
Type: System.Type
The return type of the method.
Type: System.Type[]
The types of the method's parameters.

Return Value

Type: System.Reflection.MethodInfo
The named method on an array class.


arrayClass is not an array.


arrayClass or methodName is null.

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 example demonstrates how to use GetArrayMethod to obtain the MethodInfo for a method that returns an array value.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Permissions;

public class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      AppDomain myCurrentDomain = AppDomain.CurrentDomain;
      AssemblyName myAssemblyName = new AssemblyName("TempAssembly");

      // Define a dynamic assembly in the current application domain.
      AssemblyBuilder myAssemblyBuilder = myCurrentDomain.DefineDynamicAssembly
                     (myAssemblyName, AssemblyBuilderAccess.Run);
      // Define a dynamic module in "TempAssembly" assembly.
      ModuleBuilder myModuleBuilder = myAssemblyBuilder.
      // 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);

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

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

      object myObject = Activator.CreateInstance(myType);
      MethodInfo sortArray = myType.GetMethod("SortArray");
      if (null != sortArray)
         string[] arrayToSort = { "I", "am", "not", "sorted" };
         outputBlock.Text += "Array elements before sorting " + "\n";
         for (int i = 0; i < arrayToSort.Length; i++)
            outputBlock.Text += 
               String.Format("Array element {0} : {1}\n", i, arrayToSort[i]);
         object[] arguments = { arrayToSort };
         outputBlock.Text += "Invoking the dynamically created SortArray method...\n";
         object myOutput = sortArray.Invoke(myObject, arguments);
         String[] mySortedArray = (String[])myOutput;
         outputBlock.Text += "Array elements after sorting " + "\n";
         for (int i = 0; i < mySortedArray.Length; i++)
            outputBlock.Text += 
               String.Format("Array element {0} : {1}\n", i, mySortedArray[i]);

/* This example produces the following output:

Array elements before sorting
Array element 0 : I
Array element 1 : am
Array element 2 : not
Array element 3 : sorted
Invoking the dynamically created SortArray method...
Array element 1 : am
Array element 0 : I
Array element 2 : not
Array element 3 : sorted


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

© 2015 Microsoft