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)

[ComVisibleAttribute(true)] 
public ConstructorBuilder DefineConstructor (
	MethodAttributes attributes,
	CallingConventions callingConvention,
	Type[] parameterTypes
)
/** @attribute ComVisibleAttribute(true) */ 
public ConstructorBuilder DefineConstructor (
	MethodAttributes attributes, 
	CallingConventions callingConvention, 
	Type[] parameterTypes
)
ComVisibleAttribute(true) 
public function DefineConstructor (
	attributes : MethodAttributes, 
	callingConvention : CallingConventions, 
	parameterTypes : Type[]
) : ConstructorBuilder
Not applicable.

Parameters

attributes

The attributes of the constructor.

callingConvention

The calling convention of the constructor.

parameterTypes

The parameter types of the constructor.

Return Value

The defined constructor.

Exception typeCondition

InvalidOperationException

The type was previously created using CreateType.

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 = 
   helloWorldTypeBuilder.DefineConstructor(MethodAttributes.Public, 
                      CallingConventions.Standard, constructorArgs);
// Generate IL for the method.The constructor stores its argument in the private field.
ILGenerator myConstructorIL = myConstructorBuilder.GetILGenerator();
myConstructorIL.Emit(OpCodes.Ldarg_0);
myConstructorIL.Emit(OpCodes.Ldarg_1);
myConstructorIL.Emit(OpCodes.Stfld, myGreetingField);
myConstructorIL.Emit(OpCodes.Ret);

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

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