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()

Return Value

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

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

public class Example
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
      // Define a dynamic assembly to contain the sample type. In Silverlight,
      // a dynamic assembly contains exactly one module.
      AppDomain myDomain = AppDomain.CurrentDomain;
      AssemblyName myAsmName = new AssemblyName("MakeXxxTypeExample");
      AssemblyBuilder myAssembly = myDomain.DefineDynamicAssembly(
      ModuleBuilder myModule = myAssembly.DefineDynamicModule(myAsmName.Name);

      // Define the sample type.
      TypeBuilder myType = myModule.DefineType(
          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(
          MethodAttributes.Abstract | MethodAttributes.Virtual
              | MethodAttributes.Public,

      // Create the type.
      Type t = myType.CreateType();

      outputBlock.Text += "TestMethod takes:\n";
      outputBlock.Text += "    A ref parameter of type Sample.\n";
      outputBlock.Text += "    A pointer to a Sample.\n";
      outputBlock.Text += "    A one-dimensional array of Sample objects.\n";
      outputBlock.Text += "TestMethod returns: A one-dimensional array of Sample objects.\n\n";

      outputBlock.Text += t.GetMethod("TestMethod").ToString();

/* This code produces the following output:

TestMethod takes:" & vbLf
    A ByRef parameter of type Sample.
    A pointer to a Sample.
    A one-dimensional array of Sample objects.
TestMethod returns: A one-dimensional array of Sample objects.

Sample[,] TestMethod(Sample ByRef, Sample*, Sample[])


Supported in: 5, 4, 3

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions