Export (0) Print
Expand All

ConstructorBuilder Class

Defines and represents a constructor of a dynamic class.

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

[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public sealed class ConstructorBuilder : ConstructorInfo

The ConstructorBuilder type exposes the following members.

  NameDescription
Public propertyAttributesGets the attributes for this constructor. (Overrides MethodBase.Attributes.)
Public propertyCallingConventionGets the calling convention for this constructor. (Overrides MethodBase.CallingConvention.)
Public propertyContainsGenericParametersGets a value that indicates whether the generic method contains unassigned generic type parameters. (Inherited from MethodBase.)
Public propertyDeclaringTypeGets the dynamic type that declares this constructor. (Overrides MemberInfo.DeclaringType.)
Public propertyInitLocalsGets or sets a value that specifies whether the local variables in this constructor will be zero-initialized.
Public propertyIsAbstractGets a value that indicates whether the method is abstract (MustInherit in Visual Basic). (Inherited from MethodBase.)
Public propertyIsAssemblyGets a value that indicates whether the potential visibility of this method or constructor is described by MethodAttributes.Assembly; that is, the method or constructor is visible at most to other types in the same assembly, and is not visible to derived types outside the assembly. (Inherited from MethodBase.)
Public propertyIsConstructorGets a value that indicates whether the method is a constructor. (Inherited from MethodBase.)
Public propertyIsFamilyGets a value that indicates whether the visibility of this method or constructor is described by MethodAttributes.Family; that is, the method or constructor is visible only within its class and derived classes. (Inherited from MethodBase.)
Public propertyIsFamilyAndAssemblyGets a value that indicates whether the visibility of this method or constructor is described by MethodAttributes.FamANDAssem; that is, the method or constructor can be called by derived classes, but only if they are in the same assembly. (Inherited from MethodBase.)
Public propertyIsFamilyOrAssemblyGets a value that indicates whether the potential visibility of this method or constructor is described by MethodAttributes.FamORAssem; that is, the method or constructor can be called by derived classes wherever they are, and by classes in the same assembly. (Inherited from MethodBase.)
Public propertyIsFinalGets a value that indicates whether this method is final (NotOverridable in Visual Basic). (Inherited from MethodBase.)
Public propertyIsGenericMethodGets a value that indicates whether the method is generic. (Inherited from MethodBase.)
Public propertyIsGenericMethodDefinitionGets a value that indicates whether the method is a generic method definition. (Inherited from MethodBase.)
Public propertyIsHideBySigGets a value that indicates whether only a member of the same kind with exactly the same signature is hidden in the derived class. (Inherited from MethodBase.)
Public propertyIsPrivateGets a value that indicates whether this member is private. (Inherited from MethodBase.)
Public propertyIsPublicGets a value that indicates whether this is a public method. (Inherited from MethodBase.)
Public propertyIsSpecialNameGets a value that indicates whether this method has a special name. (Inherited from MethodBase.)
Public propertyIsStaticGets a value that indicates whether the method is static (Shared in Visual Basic). (Inherited from MethodBase.)
Public propertyIsVirtualGets a value that indicates whether the method is virtual (Overridable in Visual Basic). (Inherited from MethodBase.)
Public propertyMemberTypeGets a MemberTypes value indicating that this member is a constructor. (Inherited from ConstructorInfo.)
Public propertyMetadataTokenGets a value that identifies a metadata element. (Inherited from MemberInfo.)
Public propertyMethodHandleGets the handle for the constructor. This property is not supported. (Overrides MethodBase.MethodHandle.)
Public propertyModuleGets the dynamic module in which this constructor is defined. (Overrides MemberInfo.Module.)
Public propertyNameRetrieves the name of this constructor. (Overrides MemberInfo.Name.)
Public propertyReflectedTypeGets the dynamic type on which this property is defined. (Overrides MemberInfo.ReflectedType.)
Public propertySignatureGets a string representation of the signature of the constructor.
Top

  NameDescription
Public methodDefineParameterDefines the name and attributes of a parameter of this constructor.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetCustomAttributes(Boolean)Returns all the custom attributes defined for this constructor. This method is not supported. (Overrides MemberInfo.GetCustomAttributes(Boolean).)
Public methodGetCustomAttributes(Type, Boolean)Returns the custom attributes identified by the given type. This method is not supported. (Overrides MemberInfo.GetCustomAttributes(Type, Boolean).)
Public methodGetGenericArgumentsReturns an array of Type objects that represent the type arguments of a generic method or the type parameters of a generic method definition. (Inherited from MethodBase.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetILGenerator()Returns a Microsoft intermediate language (MSIL) generator for this constructor.
Public methodGetILGenerator(Int32)Returns a Microsoft intermediate language (MSIL) generator, with the specified MSIL stream size, that can be used to build a method body for this constructor.
Public methodGetMethodImplementationFlagsReturns the method implementation flags for this constructor. (Overrides MethodBase.GetMethodImplementationFlags().)
Public methodGetModuleReturns a reference to the module that contains this constructor.
Public methodGetParametersReturns the parameters of the completed constructor. (Overrides MethodBase.GetParameters().)
Public methodGetTokenReturns the token for this constructor.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodInvoke(Object[])Invokes the current constructor with the specified arguments. (Inherited from ConstructorInfo.)
Public methodInvoke(Object, Object[])Invokes the method or constructor represented by the current instance, using the specified parameters. (Inherited from MethodBase.)
Public methodInvoke(BindingFlags, Binder, Object[], CultureInfo)Dynamically invokes the constructor reflected by this instance on the given object, passing along the specified parameters, and under the constraints of the given binder. This method is not supported. (Overrides ConstructorInfo.Invoke(BindingFlags, Binder, Object[], CultureInfo).)
Public methodInvoke(Object, BindingFlags, Binder, Object[], CultureInfo)Dynamically invokes the constructor reflected by this instance with the specified arguments, under the constraints of the specified Binder. This method is not supported. (Overrides MethodBase.Invoke(Object, BindingFlags, Binder, Object[], CultureInfo).)
Public methodIsDefinedChecks if the specified custom attribute type is defined. This method is not supported. (Overrides MemberInfo.IsDefined(Type, Boolean).)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSetCustomAttributeApplies a custom attribute to the constructor by using a custom attribute builder.
Public methodSetImplementationFlagsSets the method implementation flags for this constructor.
Public methodToStringReturns this ConstructorBuilder instance as a String. (Overrides Object.ToString().)
Top

ConstructorBuilder is used to fully describe a constructor in Microsoft intermediate language (MSIL), including the name, attributes, signature, and constructor body. It is used in conjunction with the TypeBuilder class to create classes at run time. Call DefineConstructor to get an instance of ConstructorBuilder.

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 use ConstructorBuilder to define a constructor for your dynamic type, no default constructor is provided. You have the following options for providing one:

  • If you want a default constructor that simply calls the default constructor of the base class, you can use the TypeBuilder.DefineDefaultConstructor method to create one. 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 TypeBuilder.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 a ConstructorBuilder, and provide your own implementation.

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.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Date

History

Reason

Corrected the example again.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft