Export (0) Print
Expand All

TypeBuilder.MakeByRefType Method

Returns a Type object that represents the current type when passed as a ref parameter (ByRef in Visual Basic).

Namespace: System.Reflection.Emit
Assembly: mscorlib (in mscorlib.dll)

public override Type MakeByRefType ()
public Type MakeByRefType ()
public override function MakeByRefType () : Type
Not applicable.

Return Value

A Type object that represents the current type when passed as a ref parameter (ByRef in Visual Basic).

The MakeByRefType method provides a way to generate ref types (ByRef in Visual Basic) for parameter lists.

NoteNote:

Using Microsoft intermediate language (MSIL) syntax, if the current TypeBuilder represents MyType, then the type returned by this method would be MyType&.

The following code example creates a dynamic module, an abstract type named Sample, and an abstract method named TestMethod. TestMethod takes a ref parameter (ByRef in Visual Basic) of type Sample, a pointer to type Sample, and an array of type Sample. It returns a two-dimensional array of type Sample. The code example saves the dynamic module to disk, so you can examine it with the MSIL Disassembler (Ildasm.exe).

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");
    }
}

import System.*;
import System.Reflection.*;
import System.Reflection.Emit.*;
import Microsoft.VisualBasic.*;

public class Example
{
    public static void main(String[] args)
    {
        // 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.get_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.get_Name(), myAsmName.get_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.get_Name() + ".dll"));
    } //main
} //Example

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2014 Microsoft