ConstructorBuilder.GetILGenerator Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Returns a Microsoft intermediate language (MSIL) generator for this constructor.

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

Syntax

'Declaration
Public Function GetILGenerator As ILGenerator
public ILGenerator GetILGenerator()

Return Value

Type: System.Reflection.Emit.ILGenerator
An MSIL generator that can be used to emit a method body for this constructor.

Exceptions

Exception Condition
InvalidOperationException

The constructor is a default constructor that was created by using the TypeBuilder.DefineDefaultConstructor method.

-or-

The constructor has MethodAttributes or MethodImplAttributes flags indicating that it should not have a method body.

Remarks

The runtime generates the code for default (parameterless) constructors that are created by using the TypeBuilder.DefineDefaultConstructor method. An exception is thrown if you attempt to obtain an ILGenerator for such a constructor.

Examples

The following example shows how to implement constructors in a dynamic type. The example defines a type named MyDynamicType that has two constructors: a constructor that takes an integer and sets the value of a private field named m_number, and a default (parameterless) constructor that calls the first constructor and supplies a default value for m_number. The example shows how to use OpCodes.Ldarg_0 to load the new instance of MyDynamicType, and how to call the base class constructor.

This example is part of a larger example provided for the AssemblyBuilder class.

' Add a private field of type Integer (Int32).
Dim fbNumber As FieldBuilder = tb.DefineField( _
    "m_number", _
    GetType(Integer), _
    FieldAttributes.Private)

' Define a constructor that takes an integer argument and 
' stores it in the private field. 
Dim parameterTypes() As Type = {GetType(Integer)}
Dim ctor1 As ConstructorBuilder = _
    tb.DefineConstructor( _
        MethodAttributes.Public, _
        CallingConventions.Standard, _
        parameterTypes)

Dim ctor1IL As ILGenerator = ctor1.GetILGenerator()
' For a constructor, argument zero is a reference to the new
' instance. Push it on the stack before calling the base
' class constructor. Specify the default constructor of the 
' base class (System.Object) by passing an empty array of 
' types (Type.EmptyTypes) to GetConstructor.
ctor1IL.Emit(OpCodes.Ldarg_0)
ctor1IL.Emit(OpCodes.Call, _
    GetType(Object).GetConstructor(Type.EmptyTypes))
' Push the instance on the stack before pushing the argument
' that is to be assigned to the private field m_number.
ctor1IL.Emit(OpCodes.Ldarg_0)
ctor1IL.Emit(OpCodes.Ldarg_1)
ctor1IL.Emit(OpCodes.Stfld, fbNumber)
ctor1IL.Emit(OpCodes.Ret)

' Define a default constructor that supplies a default value
' for the private field. For parameter types, pass the empty
' array of types or pass Nothing.
Dim ctor0 As ConstructorBuilder = tb.DefineConstructor( _
    MethodAttributes.Public, _
    CallingConventions.Standard, _
    Type.EmptyTypes)

Dim ctor0IL As ILGenerator = ctor0.GetILGenerator()
' For a constructor, argument zero is a reference to the new
' instance. Push it on the stack before pushing the default
' value on the stack, then call constructor ctor1.
ctor0IL.Emit(OpCodes.Ldarg_0)
ctor0IL.Emit(OpCodes.Ldc_I4_S, 42)
ctor0IL.Emit(OpCodes.Call, ctor1)
ctor0IL.Emit(OpCodes.Ret)
// Add a private field of type int (Int32).
FieldBuilder fbNumber = tb.DefineField(
    "m_number",
    typeof(int),
    FieldAttributes.Private);

// Define a constructor that takes an integer argument and 
// stores it in the private field. 
Type[] parameterTypes = { typeof(int) };
ConstructorBuilder ctor1 = tb.DefineConstructor(
    MethodAttributes.Public,
    CallingConventions.Standard,
    parameterTypes);

ILGenerator ctor1IL = ctor1.GetILGenerator();
// For a constructor, argument zero is a reference to the new
// instance. Push it on the stack before calling the base
// class constructor. Specify the default constructor of the 
// base class (System.Object) by passing an empty array of 
// types (Type.EmptyTypes) to GetConstructor.
ctor1IL.Emit(OpCodes.Ldarg_0);
ctor1IL.Emit(OpCodes.Call,
    typeof(object).GetConstructor(Type.EmptyTypes));
// Push the instance on the stack before pushing the argument
// that is to be assigned to the private field m_number.
ctor1IL.Emit(OpCodes.Ldarg_0);
ctor1IL.Emit(OpCodes.Ldarg_1);
ctor1IL.Emit(OpCodes.Stfld, fbNumber);
ctor1IL.Emit(OpCodes.Ret);

// Define a default constructor that supplies a default value
// for the private field. For parameter types, pass the empty
// array of types or pass null.
ConstructorBuilder ctor0 = tb.DefineConstructor(
    MethodAttributes.Public,
    CallingConventions.Standard,
    Type.EmptyTypes);

ILGenerator ctor0IL = ctor0.GetILGenerator();
// For a constructor, argument zero is a reference to the new
// instance. Push it on the stack before pushing the default
// value on the stack, then call constructor ctor1.
ctor0IL.Emit(OpCodes.Ldarg_0);
ctor0IL.Emit(OpCodes.Ldc_I4_S, 42);
ctor0IL.Emit(OpCodes.Call, ctor1);
ctor0IL.Emit(OpCodes.Ret);

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

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