Export (0) Print
Expand All

ConstructorBuilder.GetILGenerator Method

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

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

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.

ExceptionCondition
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.

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.

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


Silverlight

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

ADD
Show:
© 2015 Microsoft