Прочитать на английском

Поделиться через


TypeBuilder.MakeArrayType Метод

Определение

Возвращает объект Type, который представляет массив текущего типа.

Перегрузки

MakeArrayType(Int32)

Возвращает объект Type, представляющий массив текущего типа указанной размерности.

MakeArrayType()

Возвращает объект Type, представляющий одномерный массив текущего типа с нижней границей, равной нулю.

MakeArrayType(Int32)

Исходный код:
TypeBuilder.cs
Исходный код:
TypeBuilder.cs
Исходный код:
TypeBuilder.cs
Исходный код:
TypeBuilder.cs

Возвращает объект Type, представляющий массив текущего типа указанной размерности.

public override Type MakeArrayType(int rank);

Параметры

rank
Int32

Размерность массива.

Возвращаемое значение

Объект Type, который представляет одномерный массив текущего типа.

Исключения

rank не является допустимой размерностью массива.

Примеры

В следующем примере кода создается динамический модуль, абстрактный тип с именем Sampleи абстрактный метод с именем TestMethod. TestMethod ref принимает параметр (ByRef в Visual Basic) типа Sample, указатель на тип Sampleи массив типа Sample. Он возвращает двумерный массив типа Sample. В примере кода динамический модуль сохраняется на диск, поэтому вы можете изучить его с помощьюIldasm.exe (дизассемблатор IL).

using System;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.VisualBasic;

public class Example
{
    public static void Main()
    {
        // Define a dynamic assembly to contain the sample type. The
        // assembly will not be run, but only saved to disk, so
        // AssemblyBuilderAccess.Save is specified.
        //
        AppDomain myDomain = AppDomain.CurrentDomain;
        AssemblyName myAsmName = new AssemblyName("MakeXxxTypeExample");
        AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(
            myAsmName,
            AssemblyBuilderAccess.Save);

        // An assembly is made up of executable modules. For a single-
        // module assembly, the module name and file name are the same
        // as the assembly name.
        //
        ModuleBuilder myModule = myAssembly.DefineDynamicModule(
            myAsmName.Name,
            myAsmName.Name + ".dll");

        // Define the sample type.
        TypeBuilder myType = myModule.DefineType(
            "Sample",
            TypeAttributes.Public | TypeAttributes.Abstract);

        // Define a method that takes a ref argument of type Sample,
        // a pointer to type Sample, and an array of Sample objects. The
        // method returns a two-dimensional array of Sample objects.
        //
        // 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.
        //
        Type byRefMyType = myType.MakeByRefType();
        Type pointerMyType = myType.MakePointerType();
        Type arrayMyType = myType.MakeArrayType();
        Type twoDimArrayMyType = myType.MakeArrayType(2);

        // Create the array of parameter types.
        Type[] parameterTypes = {byRefMyType, pointerMyType, arrayMyType};

        // Define the abstract Test method. After you have compiled
        // and run this code example code, you can use ildasm.exe
        // to open MakeXxxTypeExample.dll, examine the Sample type,
        // and verify the parameter types and return type of the
        // TestMethod method.
        //
        MethodBuilder myMethodBuilder = myType.DefineMethod(
            "TestMethod",
            MethodAttributes.Abstract | MethodAttributes.Virtual
                | MethodAttributes.Public,
            twoDimArrayMyType,
            parameterTypes);

        // Create the type and save the assembly. For a single-file
        // assembly, there is only one module to store the manifest
        // information in.
        //
        myType.CreateType();
        myAssembly.Save(myAsmName.Name + ".dll");
    }
}

Комментарии

Метод MakeArrayType предоставляет способ создания типа массива с любым возможным типом элемента, включая универсальные типы.

См. также раздел

Применяется к

.NET 10 и другие версии
Продукт Версии
.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, 10
.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 2.0 (package-provided), 2.1

MakeArrayType()

Исходный код:
TypeBuilder.cs
Исходный код:
TypeBuilder.cs
Исходный код:
TypeBuilder.cs
Исходный код:
TypeBuilder.cs

Возвращает объект Type, представляющий одномерный массив текущего типа с нижней границей, равной нулю.

public override Type MakeArrayType();

Возвращаемое значение

Объект Type, представляющий одномерный массив текущего типа с нижней границей, равной нулю.

Примеры

В следующем примере кода создается динамический модуль, абстрактный тип с именем Sampleи абстрактный метод с именем TestMethod. TestMethod ref принимает параметр (ByRef в Visual Basic) типа Sample, указатель на тип Sampleи массив типа Sample. Он возвращает двумерный массив типа Sample. В примере кода динамический модуль сохраняется на диск, поэтому вы можете изучить его с помощьюIldasm.exe (дизассемблатор IL).

using System;
using System.Reflection;
using System.Reflection.Emit;
using Microsoft.VisualBasic;

public class Example
{
    public static void Main()
    {
        // Define a dynamic assembly to contain the sample type. The
        // assembly will not be run, but only saved to disk, so
        // AssemblyBuilderAccess.Save is specified.
        //
        AppDomain myDomain = AppDomain.CurrentDomain;
        AssemblyName myAsmName = new AssemblyName("MakeXxxTypeExample");
        AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(
            myAsmName,
            AssemblyBuilderAccess.Save);

        // An assembly is made up of executable modules. For a single-
        // module assembly, the module name and file name are the same
        // as the assembly name.
        //
        ModuleBuilder myModule = myAssembly.DefineDynamicModule(
            myAsmName.Name,
            myAsmName.Name + ".dll");

        // Define the sample type.
        TypeBuilder myType = myModule.DefineType(
            "Sample",
            TypeAttributes.Public | TypeAttributes.Abstract);

        // Define a method that takes a ref argument of type Sample,
        // a pointer to type Sample, and an array of Sample objects. The
        // method returns a two-dimensional array of Sample objects.
        //
        // 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.
        //
        Type byRefMyType = myType.MakeByRefType();
        Type pointerMyType = myType.MakePointerType();
        Type arrayMyType = myType.MakeArrayType();
        Type twoDimArrayMyType = myType.MakeArrayType(2);

        // Create the array of parameter types.
        Type[] parameterTypes = {byRefMyType, pointerMyType, arrayMyType};

        // Define the abstract Test method. After you have compiled
        // and run this code example code, you can use ildasm.exe
        // to open MakeXxxTypeExample.dll, examine the Sample type,
        // and verify the parameter types and return type of the
        // TestMethod method.
        //
        MethodBuilder myMethodBuilder = myType.DefineMethod(
            "TestMethod",
            MethodAttributes.Abstract | MethodAttributes.Virtual
                | MethodAttributes.Public,
            twoDimArrayMyType,
            parameterTypes);

        // Create the type and save the assembly. For a single-file
        // assembly, there is only one module to store the manifest
        // information in.
        //
        myType.CreateType();
        myAssembly.Save(myAsmName.Name + ".dll");
    }
}

Комментарии

Метод MakeArrayType предоставляет способ создания типа массива с любым возможным типом элемента, включая универсальные типы.

См. также раздел

Применяется к

.NET 10 и другие версии
Продукт Версии
.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, 10
.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 2.0 (package-provided), 2.1