Type.MakeArrayType 方法

定义

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

重载

MakeArrayType()

返回 Type 对象,该对象表示当前类型的一维数组(下限为零)。

MakeArrayType(Int32)

返回 Type 对象,该对象表示一个具有指定维数的当前类型的数组。

示例

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

C#
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*

 */

MakeArrayType()

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

返回 Type 对象,该对象表示当前类型的一维数组(下限为零)。

C#
public abstract Type MakeArrayType ();
C#
public virtual Type MakeArrayType ();

返回

返回一个表示当前类型的一维数组(下限为零)的 Type 对象。

例外

基类不支持调用的方法。 派生类必须提供一个实现。

当前的类型是 TypedReference

当前的类型是 ByRef 类型。 也就是说,IsByRef 返回 true

注解

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

注意 公共语言运行时区分向量 (即始终从零开始的一维数组) 和多维数组。 向量始终只有一个维度,它与恰好只有一个维度的多维数组不同。 此方法重载只能用于创建矢量类型,这是创建向量类型的唯一方法。 MakeArrayType(Int32)使用 方法重载创建多维数组类型。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

MakeArrayType(Int32)

Source:
Type.cs
Source:
Type.cs
Source:
Type.cs

返回 Type 对象,该对象表示一个具有指定维数的当前类型的数组。

C#
public abstract Type MakeArrayType (int rank);
C#
public virtual Type MakeArrayType (int rank);

参数

rank
Int32

数组的维数。 此数字必须小于或等于 32。

返回

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

例外

rank 无效。 例如,0 或负数。

基类不支持调用的方法。

当前的类型是 TypedReference

当前的类型是 ByRef 类型。 也就是说,IsByRef 返回 true

rank 大于 32。

注解

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

备注

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0