This documentation is archived and is not being maintained.

TypeBuilder.DefineDefaultConstructor Method

Defines the default constructor. The constructor defined here will simply call the default constructor of the parent.

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

[ComVisibleAttribute(true)] 
public ConstructorBuilder DefineDefaultConstructor (
	MethodAttributes attributes
)
/** @attribute ComVisibleAttribute(true) */ 
public ConstructorBuilder DefineDefaultConstructor (
	MethodAttributes attributes
)
ComVisibleAttribute(true) 
public function DefineDefaultConstructor (
	attributes : MethodAttributes
) : ConstructorBuilder

Parameters

attributes

A MethodAttributes object representing the attributes to be applied to the constructor.

Return Value

Returns the constructor.

Exception typeCondition

NotSupportedException

The parent type (base type) does not have a default constructor.

InvalidOperationException

The type was previously created using CreateType.

-or-

For the current dynamic type, the IsGenericType property is true, but the IsGenericTypeDefinition property is false.

Since the default constructor is automatically defined, it is only necessary to call this method if the attributes on the default constructor should be set to something other than PrivateScope, Public, HideBySig, SpecialName, and RTSpecialName.

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 IL 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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Show: