Share via

Facebook x.com LinkedIn Email

TypeBuilder.MakeArrayType Method (Int32)

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

Returns a Type object that represents an array of the current type, with the specified number of dimensions.

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

Syntax

'Declaration
Public Overrides Function MakeArrayType ( _
 rank As Integer _
) As Type
public override Type MakeArrayType(
 int rank
)

Parameters

  • rank
    Type: System.Int32
    The number of dimensions for the array.

Return Value

Type: System.Type
A one-dimensional array of the current type.

Exceptions

Exception Condition
IndexOutOfRangeException

rank is not a valid array dimension.

Remarks

The MakeArrayType method provides a way to generate an array type with any possible element type, including generic types.

Examples

The following example creates a dynamic module, an abstract type named Sample, and an abstract method named TestMethod. TestMethod takes a ref parameter (ByRef in Visual Basic) of type Sample, a pointer to type Sample, and an array of type Sample. It returns a two-dimensional array of type Sample.

Imports System.Reflection
Imports System.Reflection.Emit
Public Class Example
 Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
 ' Define a dynamic assembly to contain the sample type. In Silverlight,
 ' a dynamic assembly contains exactly one module.
 '
 Dim myDomain As AppDomain = AppDomain.CurrentDomain
 Dim myAsmName As New AssemblyName("MakeXxxTypeExample")
 Dim myAssembly As AssemblyBuilder = myDomain.DefineDynamicAssembly( _
 myAsmName, _
 AssemblyBuilderAccess.Run)
 Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule( _
 myAsmName.Name)
 ' Define the sample type.
 Dim myType As TypeBuilder = myModule.DefineType( _
 "Sample", _
 TypeAttributes.Public Or TypeAttributes.Abstract)
 ' Define a method that takes a ByRef argument of type Sample,
 ' a pointer to type Sample, and an array of Sample objects. The
 ' method returns a two-dimensional array of Sample objects.
 '
 ' To create this method, you need Type objects that represent the
 ' parameter types and the return type. Use the MakeByRefType, 
 ' MakePointerType, and MakeArrayType methods to create the Type
 ' objects.
 '
 Dim byRefMyType As Type = myType.MakeByRefType
 Dim pointerMyType As Type = myType.MakePointerType
 Dim arrayMyType As Type = myType.MakeArrayType
 Dim twoDimArrayMyType As Type = myType.MakeArrayType(2)
 ' Create the array of parameter types.
 Dim parameterTypes() As Type = _
 {byRefMyType, pointerMyType, arrayMyType}
 ' Define the abstract Test method.
 '
 Dim myMethodBuilder As MethodBuilder = myType.DefineMethod( _
 "TestMethod", _
 MethodAttributes.Abstract Or MethodAttributes.Virtual _
 Or MethodAttributes.Public, _
 twoDimArrayMyType, _
 parameterTypes)
 ' Create the type.
 '
 Dim t As Type = myType.CreateType()
 outputBlock.Text &= "TestMethod takes:" & vbLf
 outputBlock.Text &= " A ByRef parameter of type Sample." & vbLf
 outputBlock.Text &= " A pointer to a Sample." & vbLf
 outputBlock.Text &= " A one-dimensional array of Sample objects." & vbLf
 outputBlock.Text &= "TestMethod returns: A one-dimensional array of Sample objects." & vbLf &vbLf
 outputBlock.Text &= t.GetMethod("TestMethod").ToString()
 End Sub
End Class
' This code produces the following output:
'
'TestMethod takes:" & vbLf
' A ByRef parameter of type Sample.
' A pointer to a Sample.
' A one-dimensional array of Sample objects.
'TestMethod returns: A one-dimensional array of Sample objects.
'
'Sample[,] TestMethod(Sample ByRef, Sample*, Sample[])
using System;
using System.Reflection;
using System.Reflection.Emit;
//using Microsoft.VisualBasic;
public class Example
{
 public static void Demo(System.Windows.Controls.TextBlock outputBlock)
 {
 // Define a dynamic assembly to contain the sample type. In Silverlight,
 // a dynamic assembly contains exactly one module.
 //
 AppDomain myDomain = AppDomain.CurrentDomain;
 AssemblyName myAsmName = new AssemblyName("MakeXxxTypeExample");
 AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(
 myAsmName,
 AssemblyBuilderAccess.Run);
 ModuleBuilder myModule = myAssembly.DefineDynamicModule(myAsmName.Name);
 // Define the sample type.
 TypeBuilder myType = myModule.DefineType(
 "Sample",
 TypeAttributes.Public | TypeAttributes.Abstract);
 // Define a method that takes a ref argument of type Sample,
 // a pointer to type Sample, and an array of Sample objects. The
 // method returns a two-dimensional array of Sample objects.
 //
 // To create this method, you need Type objects that represent the
 // parameter types and the return type. Use the MakeByRefType, 
 // MakePointerType, and MakeArrayType methods to create the Type
 // objects.
 //
 Type byRefMyType = myType.MakeByRefType();
 Type pointerMyType = myType.MakePointerType();
 Type arrayMyType = myType.MakeArrayType();
 Type twoDimArrayMyType = myType.MakeArrayType(2);
 // Create the array of parameter types.
 Type[] parameterTypes = { byRefMyType, pointerMyType, arrayMyType };
 // Define the abstract Test method. After you have compiled
 // and run this code example code, you can use ildasm.exe 
 // to open MakeXxxTypeExample.dll, examine the Sample type,
 // and verify the parameter types and return type of the
 // TestMethod method.
 //
 MethodBuilder myMethodBuilder = myType.DefineMethod(
 "TestMethod",
 MethodAttributes.Abstract | MethodAttributes.Virtual
 | MethodAttributes.Public,
 twoDimArrayMyType,
 parameterTypes);
 // Create the type.
 //
 Type t = myType.CreateType();
 outputBlock.Text += "TestMethod takes:\n";
 outputBlock.Text += " A ref parameter of type Sample.\n";
 outputBlock.Text += " A pointer to a Sample.\n";
 outputBlock.Text += " A one-dimensional array of Sample objects.\n";
 outputBlock.Text += "TestMethod returns: A one-dimensional array of Sample objects.\n\n";
 outputBlock.Text += t.GetMethod("TestMethod").ToString();
 }
}
/* This code produces the following output:
TestMethod takes:" & vbLf
 A ByRef parameter of type Sample.
 A pointer to a Sample.
 A one-dimensional array of Sample objects.
TestMethod returns: A one-dimensional array of Sample objects.
Sample[,] TestMethod(Sample ByRef, Sample*, Sample[])
 */

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.


  • Last updated on 2011年11月17日