TypeBuilder.MakeByRefType 方法

定义

返回一个 Type 对象,它在作为 ref 参数(Visual Basic 中的ByRef )传递时表示当前类型。

public:
 override Type ^ MakeByRefType();
public override Type MakeByRefType ();
override this.MakeByRefType : unit -> Type
Public Overrides Function MakeByRefType () As Type

返回

Type 对象,它在作为 ref 参数(Visual Basic 中的 ByRef)传递时表示当前类型。

示例

下面的代码示例创建一个动态模块、一个名为 的 Sample抽象类型和一个名为 的 TestMethod抽象方法。 TestMethodref在 Visual Basic) 中采用参数 (ByRef类型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");
    }
}
Imports System.Reflection
Imports System.Reflection.Emit

Public Class Example
    Public Shared Sub 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.
        '
        Dim myDomain As AppDomain = AppDomain.CurrentDomain
        Dim myAsmName As New AssemblyName("MakeXxxTypeExample")
        Dim myAssembly As AssemblyBuilder = 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. 
        '
        Dim myModule As ModuleBuilder = myAssembly.DefineDynamicModule( _
            myAsmName.Name, _
            myAsmName.Name & ".dll")

        ' Define the sample type.
        Dim myType As TypeBuilder = myModule.DefineType( _
            "Sample", _
            TypeAttributes.Public Or TypeAttributes.Abstract)

        ' Define a method that takes a ByRef 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.
        '
        Dim byRefMyType As Type = myType.MakeByRefType
        Dim pointerMyType As Type = myType.MakePointerType
        Dim arrayMyType As Type = myType.MakeArrayType
        Dim twoDimArrayMyType As Type = myType.MakeArrayType(2)

        ' Create the array of parameter types.
        Dim parameterTypes() As Type = _
            {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.
        '
        Dim myMethodBuilder As MethodBuilder = myType.DefineMethod( _
            "TestMethod", _
            MethodAttributes.Abstract Or MethodAttributes.Virtual _
                Or 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")

    End Sub
End Class

注解

方法MakeByRefType提供了一种在 Visual Basic) ByRef 中为参数列表生成ref类型 (的方法。

注意

使用Microsoft中间语言 (MSIL) 语法,如果当前 TypeBuilder 表示 MyType,则此方法返回的类型为 MyType&

适用于

另请参阅