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 Overrides Function MakeByRefType As Type

Return Value

Type: System.Type
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.


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 Ildasm.exe (IL Disassembler).

Imports System
Imports System.Reflection
Imports System.Reflection.Emit
Imports Microsoft.VisualBasic

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, _

        ' 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, _

        ' Create the type and save the assembly. For a single-file  
        ' assembly, there is only one module to store the manifest  
        ' information in. 
        myAssembly.Save(myAsmName.Name & ".dll")

    End Sub 
End Class

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
© 2015 Microsoft