GenericTypeParameterBuilder.MakeArrayType Method (Int32)

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

Returns the type of an array whose element type is the generic type parameter, 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 Type object that represents the type of an array whose element type is the generic type parameter, with the specified number of dimensions.

Exceptions

Exception Condition
IndexOutOfRangeException

rank is not a valid number of dimensions. For example, its value is less than 1.

Remarks

The MakeArrayType method provides a way to generate array types for parameter lists.

Examples

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

' Define a dynamic assembly to contain the sample type. 
'
Dim myDomain As AppDomain = AppDomain.CurrentDomain
Dim myAsmName As New AssemblyName("MakeXxxGenericTypeParameterExample")
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)

' Make the sample type a generic type, by defining a type
' parameter T. All type parameters are defined at the same
' time, by passing an array containing the type parameter
' names. 
Dim typeParamNames() As String = {"T"}
Dim typeParams() As GenericTypeParameterBuilder = _
    myType.DefineGenericParameters(typeParamNames)

' Define a method that takes a ByRef argument of type T, a
' pointer to type T, and one-dimensional array of type T. The
' method returns a two-dimensional array of type T.
'
' 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, using the generic type parameter T.
'
Dim byRefType As Type = typeParams(0).MakeByRefType
Dim pointerType As Type = typeParams(0).MakePointerType
Dim arrayType As Type = typeParams(0).MakeArrayType
Dim twoDimArrayType As Type = typeParams(0).MakeArrayType(2)

' Create the array of parameter types.
Dim parameterTypes() As Type = _
    {byRefType, pointerType, arrayType}

' Define the abstract Test method. 
'
Dim myMethodBuilder As MethodBuilder = myType.DefineMethod( _
    "TestMethod", _
    MethodAttributes.Abstract Or MethodAttributes.Virtual _
        Or MethodAttributes.Public, _
    twoDimArrayType, _
    parameterTypes)

' Create the type.
'
myType.CreateType()
// Define a dynamic assembly to contain the sample type. 
//
AppDomain myDomain = AppDomain.CurrentDomain;
AssemblyName myAsmName = new
    AssemblyName("MakeXxxGenericTypeParameterExample");
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);

// Make the sample type a generic type, by defining a type
// parameter T. All type parameters are defined at the same
// time, by passing an array containing the type parameter
// names. 
string[] typeParamNames = { "T" };
GenericTypeParameterBuilder[] typeParams =
    myType.DefineGenericParameters(typeParamNames);

// Define a method that takes a ByRef argument of type T, a
// pointer to type T, and one-dimensional array of type T. The
// method returns a two-dimensional array of type T.
//
// 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, using the generic type parameter T.
//
Type byRefType = typeParams[0].MakeByRefType();
Type pointerType = typeParams[0].MakePointerType();
Type arrayType = typeParams[0].MakeArrayType();
Type twoDimArrayType = typeParams[0].MakeArrayType(2);

// Create the array of parameter types.
Type[] parameterTypes = { byRefType, pointerType, arrayType };

// Define the abstract Test method. 
//
MethodBuilder myMethodBuilder = myType.DefineMethod(
    "TestMethod",
    MethodAttributes.Abstract | MethodAttributes.Virtual
    | MethodAttributes.Public,
    twoDimArrayType,
    parameterTypes);

// Create the type.
//
myType.CreateType();

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.