This documentation is archived and is not being maintained.

TypeBuilder.DefineConstructor Method (MethodAttributes, CallingConventions, Type[])

Adds a new constructor to the type, with the given attributes and signature.

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

public ConstructorBuilder DefineConstructor(
	MethodAttributes attributes,
	CallingConventions callingConvention,
	Type[] parameterTypes


Type: System.Reflection.MethodAttributes

The attributes of the constructor.

Type: System.Reflection.CallingConventions

The calling convention of the constructor.

Type: System.Type[]

The parameter types of the constructor.

Return Value

Type: System.Reflection.Emit.ConstructorBuilder
The defined constructor.


The type was previously created using CreateType.

If you do not define a constructor for your dynamic type, a default constructor is provided automatically, and it calls the default constructor of the base class.

If you define a constructor for your dynamic type, a default constructor is not provided. You have the following options for providing a default constructor in addition to the constructor you defined:

  • If you want a default constructor that simply calls the default constructor of the base class, you can use the DefineDefaultConstructor method to create one (and optionally restrict access to it). Do not provide an implementation for this default constructor. If you do, an exception is thrown when you try to use the constructor. No exception is thrown when the CreateType method is called.

  • If you want a default constructor that does something more than simply calling the default constructor of the base class, or that calls another constructor of the base class, or that does something else entirely, you must use the TypeBuilder.DefineConstructor method to create one, and provide your own implementation.

The following code sample demonstrates the use of DefineConstructor to set a constructor's particular signature and attributes on a dynamic type and return a corresponding ConstructorBuilder for MSIL population.

// Define the constructor.
Type[] constructorArgs = { typeof(String) };
ConstructorBuilder myConstructorBuilder = 
                      CallingConventions.Standard, constructorArgs);
// Generate IL for the method.The constructor stores its argument in the private field.
ILGenerator myConstructorIL = myConstructorBuilder.GetILGenerator();
myConstructorIL.Emit(OpCodes.Stfld, myGreetingField);

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0