다음을 통해 공유


ModuleBuilder.GetArrayMethod Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns the named method on an array class.

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

Syntax

'Declaration
<SecuritySafeCriticalAttribute> _
Public Function GetArrayMethod ( _
    arrayClass As Type, _
    methodName As String, _
    callingConvention As CallingConventions, _
    returnType As Type, _
    parameterTypes As Type() _
) As MethodInfo
[SecuritySafeCriticalAttribute]
public MethodInfo GetArrayMethod(
    Type arrayClass,
    string methodName,
    CallingConventions callingConvention,
    Type returnType,
    Type[] parameterTypes
)

Parameters

  • methodName
    Type: System.String
    The name of a method on the array class.
  • returnType
    Type: System.Type
    The return type of the method.
  • parameterTypes
    Type: array<System.Type[]
    The types of the method's parameters.

Return Value

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

Exceptions

Exception Condition
ArgumentException

arrayClass is not an array.

ArgumentNullException

arrayClass or methodName is nulla 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.

Examples

The following example demonstrates how to use GetArrayMethod to obtain the MethodInfo for a method that returns an array value.

Imports System.Reflection
Imports System.Reflection.Emit
Imports System.Security.Permissions

Public Class Example

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Dim myCurrentDomain As AppDomain = AppDomain.CurrentDomain
      Dim myAssemblyName As New AssemblyName("TempAssembly")

      ' Define a dynamic assembly in the current application domain.
      Dim myAssemblyBuilder As AssemblyBuilder = _
            myCurrentDomain.DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.Run)
      ' 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)

      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.
      Dim myType As Type = myTypeBuilder.CreateType()

      Dim myObject As Object = Activator.CreateInstance(myType)
      Dim sortArray As MethodInfo = myType.GetMethod("SortArray")
      If Not sortArray Is Nothing Then
         Dim arrayToSort As String() = {"I", "am", "not", "sorted"}
         outputBlock.Text &= "Array elements before sorting " & vbCrLf
         Dim i As Integer
         For i = 0 To arrayToSort.Length - 1
            outputBlock.Text &= String.Format("Array element {0} : {1}" & vbCrLf, _
               i, arrayToSort(i))
         Next i
         Dim arguments() As Object = New Object() {arrayToSort}
         outputBlock.Text &= "Invoking our dynamically " + _
                           "created SortArray method..." & vbCrLf
         Dim myOutput As Object = sortArray.Invoke(myObject, arguments)
         Dim mySortedArray As String() = CType(myOutput, String())
         outputBlock.Text &= "Array elements after sorting " & vbCrLf
         Dim j As Integer
         For j = 0 To mySortedArray.Length - 1
            outputBlock.Text &= String.Format("Array element {0} : {1}" & vbCrLf, _
               j, mySortedArray(j)) 
         Next j
      End If
   End Sub 
End Class 

' 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
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.
                                    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);

      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.
      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
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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