Compartilhar via


ConstructorBuilder Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Defines and represents a constructor of a dynamic class.

Inheritance Hierarchy

System.Object
  System.Reflection.MemberInfo
    System.Reflection.MethodBase
      System.Reflection.ConstructorInfo
        System.Reflection.Emit.ConstructorBuilder

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

Syntax

'Declaration
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
Public NotInheritable Class ConstructorBuilder _
    Inherits ConstructorInfo
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public sealed class ConstructorBuilder : ConstructorInfo

The ConstructorBuilder type exposes the following members.

Properties

  Name Description
Public property Attributes Gets the attributes for this constructor. (Overrides MethodBase.Attributes.)
Public property CallingConvention Gets the calling convention for this constructor. (Overrides MethodBase.CallingConvention.)
Public property ContainsGenericParameters Gets a value that indicates whether the generic method contains unassigned generic type parameters. (Inherited from MethodBase.)
Public property DeclaringType Gets the dynamic type that declares this constructor. (Overrides MemberInfo.DeclaringType.)
Public property InitLocals Gets or sets a value that specifies whether the local variables in this constructor will be zero-initialized.
Public property IsAbstract Gets a value that indicates whether the method is abstract (MustInherit in Visual Basic). (Inherited from MethodBase.)
Public property IsAssembly Gets 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 property IsConstructor Gets a value that indicates whether the method is a constructor. (Inherited from MethodBase.)
Public property IsFamily Gets 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 property IsFamilyAndAssembly Gets 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 property IsFamilyOrAssembly Gets 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 property IsFinal Gets a value that indicates whether this method is final (NotOverridable in Visual Basic). (Inherited from MethodBase.)
Public property IsGenericMethod Gets a value that indicates whether the method is generic. (Inherited from MethodBase.)
Public property IsGenericMethodDefinition Gets a value that indicates whether the method is a generic method definition. (Inherited from MethodBase.)
Public property IsHideBySig Gets 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 property IsPrivate Gets a value that indicates whether this member is private. (Inherited from MethodBase.)
Public property IsPublic Gets a value that indicates whether this is a public method. (Inherited from MethodBase.)
Public property IsSpecialName Gets a value that indicates whether this method has a special name. (Inherited from MethodBase.)
Public property IsStatic Gets a value that indicates whether the method is static (Shared in Visual Basic). (Inherited from MethodBase.)
Public property IsVirtual Gets a value that indicates whether the method is virtual (Overridable in Visual Basic). (Inherited from MethodBase.)
Public property MemberType Gets a MemberTypes value indicating that this member is a constructor. (Inherited from ConstructorInfo.)
Public property MetadataToken Gets a value that identifies a metadata element. (Inherited from MemberInfo.)
Public property MethodHandle Gets the handle for the constructor. This property is not supported. (Overrides MethodBase.MethodHandle.)
Public property Module Gets the dynamic module in which this constructor is defined. (Overrides MemberInfo.Module.)
Public property Name Retrieves the name of this constructor. (Overrides MemberInfo.Name.)
Public property ReflectedType Gets the dynamic type on which this property is defined. (Overrides MemberInfo.ReflectedType.)
Public property Signature Gets a string representation of the signature of the constructor.

Top

Methods

  Name Description
Public method DefineParameter Defines the name and attributes of a parameter of this constructor.
Public method Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public method GetCustomAttributes(Boolean) Returns all the custom attributes defined for this constructor. This method is not supported. (Overrides MemberInfo.GetCustomAttributes(Boolean).)
Public method GetCustomAttributes(Type, Boolean) Returns the custom attributes identified by the given type. This method is not supported. (Overrides MemberInfo.GetCustomAttributes(Type, Boolean).)
Public method GetGenericArguments Returns 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 method GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public method GetILGenerator() Returns a Microsoft intermediate language (MSIL) generator for this constructor.
Public method GetILGenerator(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 method GetMethodImplementationFlags Returns the method implementation flags for this constructor. (Overrides MethodBase.GetMethodImplementationFlags().)
Public method GetModule Returns a reference to the module that contains this constructor.
Public method GetParameters Returns the parameters of the completed constructor. (Overrides MethodBase.GetParameters().)
Public method GetToken Returns the token for this constructor.
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Invoke(array<Object[]) Invokes the current constructor with the specified arguments. (Inherited from ConstructorInfo.)
Public method Invoke(Object, array<Object[]) Invokes the method or constructor represented by the current instance, using the specified parameters. (Inherited from MethodBase.)
Public method Invoke(BindingFlags, Binder, array<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, array<Object[], CultureInfo).)
Public method Invoke(Object, BindingFlags, Binder, array<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, array<Object[], CultureInfo).)
Public method IsDefined Checks if the specified custom attribute type is defined. This method is not supported. (Overrides MemberInfo.IsDefined(Type, Boolean).)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method SetCustomAttribute Applies a custom attribute to the constructor by using a custom attribute builder.
Public method SetImplementationFlags Sets the method implementation flags for this constructor.
Public method ToString Returns this ConstructorBuilder instance as a String. (Overrides Object.ToString().)

Top

Remarks

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.

Examples

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 Integer (Int32).
Dim fbNumber As FieldBuilder = tb.DefineField( _
    "m_number", _
    GetType(Integer), _
    FieldAttributes.Private)

' Define a constructor that takes an integer argument and 
' stores it in the private field. 
Dim parameterTypes() As Type = {GetType(Integer)}
Dim ctor1 As ConstructorBuilder = _
    tb.DefineConstructor( _
        MethodAttributes.Public, _
        CallingConventions.Standard, _
        parameterTypes)

Dim ctor1IL As ILGenerator = 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, _
    GetType(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 Nothing.
Dim ctor0 As ConstructorBuilder = tb.DefineConstructor( _
    MethodAttributes.Public, _
    CallingConventions.Standard, _
    Type.EmptyTypes)

Dim ctor0IL As ILGenerator = 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)
// 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);

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

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

Change History

Date

History

Reason

Corrected the example again.

Customer feedback.