Type.MakeArrayType 方法

定義

傳回代表目前類型之陣列的 Type 物件。

多載

MakeArrayType()

傳回 Type 物件,代表由目前類型組成的一維陣列,其下限為零。

MakeArrayType(Int32)

傳回 Type 物件,代表由目前類型組成且為指定維度個數的陣列。

範例

下列程式代碼範例會在 Visual Basic) 中建立陣列、 ref (ByRef ,以及類別的 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()

來源:
Type.cs
來源:
Type.cs
來源:
Type.cs

傳回 Type 物件,代表由目前類型組成的一維陣列,其下限為零。

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

傳回

Type 物件,代表由目前類型組成的一維陣列,其下限為零。

例外狀況

基底類別不支援叫用的方法。 衍生類別必須提供實作。

目前的類型為 TypedReference

-或-

目前的類型為 ByRef 類型。 亦即,IsByRef 會傳回 true

備註

方法 MakeArrayType 提供方法來產生數位類型,其專案類型是在運行時間計算。

注意 Common Language Runtime 會區分向量 (,也就是一律以零為基底) 和多維度陣列的一維數位列。 一律只有一個維度的向量,與只具有一個維度的多維度陣列不同。 這個方法多載只能用來建立向量類型,而且它是建立向量類型的唯一方法。 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)

來源:
Type.cs
來源:
Type.cs
來源:
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 提供方法來產生數位類型,其專案類型是在運行時間計算。

備註

Common Language Runtime 會區分向量 (,也就是一律以零為基底) 和多維度陣列的一維數位列。 一律只有一個維度的向量,與只具有一個維度的多維度陣列不同。 您無法使用這個方法多載來建立向量類型;如果 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