System.Type.MakeArrayType 方法 (Int32)

方法描述

返回表示当前类型的指定维数的数组的 Type 对象。

语法定义(C# System.Type.MakeArrayType 方法 (Int32) 的用法)

public virtual Type MakeArrayType(
	int rank
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
rank System-Int32 数组的维数。该数字必须小于或等于 32。
返回值 System.Type 表示当前类型的指定维数的数组的对象。

提示和注释

MakeArrayType 方法提供了一种生成元素类型在运行时计算的数组类型的方式。

注意

公共语言运行时对向量(即始终从零开始的一维数组)和多维数组进行了区分。 向量始终只有一个维度,它与恰好只有一个维度的多维数组不同。 不能使用此方法重载来创建向量类型;如果 rank 为 1,则此方法重载返回恰巧具有一维的多维数组类型。 请使用 MakeArrayType() 方法重载来创建向量类型。

System.Type.MakeArrayType 方法 (Int32)例子

下面的代码示例为 Test 类创建数组、ref(在 Visual Basic 中为 ByRef)和指针类型。

using System;
using System.Reflection;

public class Example
{
    public static void Main()
    {
        // Create a Type object that represents a one-dimensional
        // array of Example objects.
        Type t = typeof(Example).MakeArrayType();
        Console.WriteLine("\r\nArray of Example: {0}", t);

        // Create a Type object that represents a two-dimensional
        // array of Example objects.
        t = typeof(Example).MakeArrayType(2);
        Console.WriteLine("\r\nTwo-dimensional array of Example: {0}", t);

        // Demonstrate an exception when an invalid array rank is
        // specified.
        try
        {
            t = typeof(Example).MakeArrayType(-1);
        }
        catch(Exception ex)
        {
            Console.WriteLine("\r\n{0}", ex);
        }

        // Create a Type object that represents a ByRef parameter
        // of type Example.
        t = typeof(Example).MakeByRefType();
        Console.WriteLine("\r\nByRef Example: {0}", t);

        // Get a Type object representing the Example class, a
        // MethodInfo representing the "Test" method, a ParameterInfo
        // representing the parameter of type Example, and finally
        // a Type object representing the type of this ByRef parameter.
        // Compare this Type object with the Type object created using
        // MakeByRefType.
        Type t2 = typeof(Example);
        MethodInfo mi = t2.GetMethod("Test");
        ParameterInfo pi = mi.GetParameters()[0];
        Type pt = pi.ParameterType;
        Console.WriteLine("Are the ByRef types equal? {0}", (t == pt));

        // Create a Type object that represents a pointer to an
        // Example object.
        t = typeof(Example).MakePointerType();
        Console.WriteLine("\r\nPointer to Example: {0}", t);
    }

    // A sample method with a ByRef parameter.
    //
    public void Test(ref Example e)
    {
    }
}

/* This example produces output similar to the following:

Array of Example: Example[]

Two-dimensional array of Example: Example[,]

System.IndexOutOfRangeException: Index was outside the bounds of the array.
   at System.RuntimeType.MakeArrayType(Int32 rank) in c:\vbl\ndp\clr\src\BCL\System\RtType.cs:line 2999
   at Example.Main()

ByRef Example: Example&
Are the ByRef types equal? True

Pointer to Example: Example*

 */

异常

异常 异常描述
IndexOutOfRangeException rank 无效。 例如,0 或负数。
NotSupportedException 基类不支持所调用的方法。
TypeLoadException
  • 当前类型为 TypedReference。
  • 当前类型是 ByRef 类型。 即,Type.IsByRef 返回 true。
  • rank 大于 32。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。