Leer en inglés

Compartir a través de


Type.MakeArrayType Método

Definición

Devuelve un objeto Type que representa una matriz del tipo actual.

Sobrecargas

MakeArrayType()

Devuelve un objeto Type que representa una matriz unidimensional del tipo actual, con un límite inferior de cero.

MakeArrayType(Int32)

Devuelve un objeto Type que representa una matriz del tipo actual, con el número de dimensiones especificado.

Ejemplos

En el ejemplo de código siguiente se crea una matriz , ref (ByRef en Visual Basic) y tipos de puntero para la Test clase .

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

Devuelve un objeto Type que representa una matriz unidimensional del tipo actual, con un límite inferior de cero.

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

Devoluciones

Objeto Type que representa una matriz unidimensional del tipo actual, con un límite inferior de cero.

Excepciones

El método invocado no se admite en la clase base. Las clases derivadas deben proporcionar una implementación.

El tipo actual es TypedReference.

O bien

El tipo actual es un tipo ByRef. Es decir, IsByRef devuelve true.

Comentarios

El MakeArrayType método proporciona una manera de generar tipos de matriz cuyos tipos de elemento se calculan en tiempo de ejecución.

Nota Common Language Runtime distingue entre vectores (es decir, matrices unidimensionales que siempre están basadas en cero) y matrices multidimensionales. Un vector, que siempre tiene una sola dimensión, no es la misma que una matriz multidimensional que tiene solo una dimensión. Esta sobrecarga de método solo se puede usar para crear tipos de vector y es la única manera de crear un tipo de vector. Use la sobrecarga del MakeArrayType(Int32) método para crear tipos de matriz multidimensional.

Consulte también

Se aplica a

.NET 9 y otras versiones
Producto Versiones
.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

Devuelve un objeto Type que representa una matriz del tipo actual, con el número de dimensiones especificado.

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

Parámetros

rank
Int32

Número de dimensiones de la matriz. Este número debe ser menor o igual que 32.

Devoluciones

Objeto que representa una matriz del tipo actual, con el número de dimensiones especificado.

Excepciones

rank no es válido. Por ejemplo, 0 o negativo.

No se admite el método invocado en la clase base.

El tipo actual es TypedReference.

O bien

El tipo actual es un tipo ByRef. Es decir, IsByRef devuelve true.

O bien

rank es mayor que 32.

Comentarios

El MakeArrayType método proporciona una manera de generar tipos de matriz cuyos tipos de elemento se calculan en tiempo de ejecución.

Nota

Common Language Runtime distingue entre vectores (es decir, matrices unidimensionales que siempre están basadas en cero) y matrices multidimensionales. Un vector, que siempre tiene una sola dimensión, no es la misma que una matriz multidimensional que tiene solo una dimensión. No se puede usar esta sobrecarga de método para crear un tipo vectorial; Si rank es 1, esta sobrecarga de método devuelve un tipo de matriz multidimensional que tiene una dimensión. Use la sobrecarga del MakeArrayType() método para crear tipos de vector.

Consulte también

Se aplica a

.NET 9 y otras versiones
Producto Versiones
.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