CodeConstructor Class
TOC
Collapse the table of content
Expand the table of content

CodeConstructor Class

 

Represents a declaration for an instance constructor of a type.

Namespace:   System.CodeDom
Assembly:  System (in System.dll)

System.Object
  System.CodeDom.CodeObject
    System.CodeDom.CodeTypeMember
      System.CodeDom.CodeMemberMethod
        System.CodeDom.CodeConstructor

[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
[ComVisibleAttribute(true)]
public class CodeConstructor : CodeMemberMethod

NameDescription
System_CAPS_pubmethodCodeConstructor()

Initializes a new instance of the CodeConstructor class.

NameDescription
System_CAPS_pubpropertyAttributes

Gets or sets the attributes of the member.(Inherited from CodeTypeMember.)

System_CAPS_pubpropertyBaseConstructorArgs

Gets the collection of base constructor arguments.

System_CAPS_pubpropertyChainedConstructorArgs

Gets the collection of chained constructor arguments.

System_CAPS_pubpropertyComments

Gets the collection of comments for the type member.(Inherited from CodeTypeMember.)

System_CAPS_pubpropertyCustomAttributes

Gets or sets the custom attributes of the member.(Inherited from CodeTypeMember.)

System_CAPS_pubpropertyEndDirectives

Gets the end directives for the member.(Inherited from CodeTypeMember.)

System_CAPS_pubpropertyImplementationTypes

Gets the data types of the interfaces implemented by this method, unless it is a private method implementation, which is indicated by the PrivateImplementationType property.(Inherited from CodeMemberMethod.)

System_CAPS_pubpropertyLinePragma

Gets or sets the line on which the type member statement occurs.(Inherited from CodeTypeMember.)

System_CAPS_pubpropertyName

Gets or sets the name of the member.(Inherited from CodeTypeMember.)

System_CAPS_pubpropertyParameters

Gets the parameter declarations for the method.(Inherited from CodeMemberMethod.)

System_CAPS_pubpropertyPrivateImplementationType

Gets or sets the data type of the interface this method, if private, implements a method of, if any.(Inherited from CodeMemberMethod.)

System_CAPS_pubpropertyReturnType

Gets or sets the data type of the return value of the method.(Inherited from CodeMemberMethod.)

System_CAPS_pubpropertyReturnTypeCustomAttributes

Gets the custom attributes of the return type of the method.(Inherited from CodeMemberMethod.)

System_CAPS_pubpropertyStartDirectives

Gets the start directives for the member.(Inherited from CodeTypeMember.)

System_CAPS_pubpropertyStatements

Gets the statements within the method.(Inherited from CodeMemberMethod.)

System_CAPS_pubpropertyTypeParameters

Gets the type parameters for the current generic method.(Inherited from CodeMemberMethod.)

System_CAPS_pubpropertyUserData

Gets the user-definable data for the current object.(Inherited from CodeObject.)

NameDescription
System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubeventPopulateImplementationTypes

An event that will be raised the first time the ImplementationTypes collection is accessed.(Inherited from CodeMemberMethod.)

System_CAPS_pubeventPopulateParameters

An event that will be raised the first time the Parameters collection is accessed.(Inherited from CodeMemberMethod.)

System_CAPS_pubeventPopulateStatements

An event that will be raised the first time the Statements collection is accessed.(Inherited from CodeMemberMethod.)

CodeConstructor can be used to represent a declaration of an instance constructor for a type. Use CodeTypeConstructor to declare a static constructor for a type.

If the constructor calls a base class constructor, set the constructor arguments for the base class constructor in the BaseConstructorArgs property. If the constructor overloads another constuctor for the type, set the constructor arguments to pass to the overloaded type constructor in the ChainedConstructorArgs property.

This example demonstrates using a CodeConstructor to declare several types of constructors.

// This example declares two types, one of which inherits from another,
// and creates a set of different styles of constructors using CodeConstructor.

// Creates a new CodeCompileUnit to contain the program graph.
CodeCompileUnit CompileUnit = new CodeCompileUnit();
// Declares a new namespace object and names it.
CodeNamespace Samples = new CodeNamespace("Samples");
// Adds the namespace object to the compile unit.
CompileUnit.Namespaces.Add( Samples );
// Adds a new namespace import for the System namespace.
Samples.Imports.Add( new CodeNamespaceImport("System") );            

// Declares a new type and names it.
CodeTypeDeclaration BaseType = new CodeTypeDeclaration("BaseType");                                                
// Adds the new type to the namespace object's type collection.
Samples.Types.Add(BaseType);

// Declares a default constructor that takes no arguments.
CodeConstructor defaultConstructor = new CodeConstructor();
defaultConstructor.Attributes = MemberAttributes.Public;
// Adds the constructor to the Members collection of the BaseType.
BaseType.Members.Add(defaultConstructor);

// Declares a constructor that takes a string argument.
CodeConstructor stringConstructor = new CodeConstructor();
stringConstructor.Attributes = MemberAttributes.Public;
// Declares a parameter of type string named "TestStringParameter".
stringConstructor.Parameters.Add( new CodeParameterDeclarationExpression("System.String", "TestStringParameter") );
// Adds the constructor to the Members collection of the BaseType.
BaseType.Members.Add(stringConstructor);

// Declares a type that derives from BaseType and names it.
CodeTypeDeclaration DerivedType = new CodeTypeDeclaration("DerivedType");
// The DerivedType class inherits from the BaseType class.
DerivedType.BaseTypes.Add( new CodeTypeReference("BaseType") );
// Adds the new type to the namespace object's type collection.
Samples.Types.Add(DerivedType);        

// Declare a constructor that takes a string argument and calls the base class constructor with it.
CodeConstructor baseStringConstructor = new CodeConstructor();
baseStringConstructor.Attributes = MemberAttributes.Public;
// Declares a parameter of type string named "TestStringParameter".    
baseStringConstructor.Parameters.Add( new CodeParameterDeclarationExpression("System.String", "TestStringParameter") );
// Calls a base class constructor with the TestStringParameter parameter.
baseStringConstructor.BaseConstructorArgs.Add( new CodeVariableReferenceExpression("TestStringParameter") );
// Adds the constructor to the Members collection of the DerivedType.
DerivedType.Members.Add(baseStringConstructor);

// Declares a constructor overload that calls another constructor for the type with a predefined argument.
CodeConstructor overloadConstructor = new CodeConstructor();
overloadConstructor.Attributes = MemberAttributes.Public;
// Sets the argument to pass to a base constructor method.
overloadConstructor.ChainedConstructorArgs.Add( new CodePrimitiveExpression("Test") );
// Adds the constructor to the Members collection of the DerivedType.
DerivedType.Members.Add(overloadConstructor);        

// Declares a constructor overload that calls the default constructor for the type.
CodeConstructor overloadConstructor2 = new CodeConstructor();
overloadConstructor2.Attributes = MemberAttributes.Public;
overloadConstructor2.Parameters.Add( new CodeParameterDeclarationExpression("System.Int32", "TestIntParameter") );
// Sets the argument to pass to a base constructor method.
overloadConstructor2.ChainedConstructorArgs.Add( new CodeSnippetExpression("") );
// Adds the constructor to the Members collection of the DerivedType.
DerivedType.Members.Add(overloadConstructor2);            

// A C# code generator produces the following source code for the preceeding example code:

// public class BaseType {
//     
//     public BaseType() {
//     }
//        
//     public BaseType(string TestStringParameter) {
//     }
// }
//    
// public class DerivedType : BaseType {
//        
//     public DerivedType(string TestStringParameter) : 
//             base(TestStringParameter) {
//     }
//        
//     public DerivedType() : 
//             this("Test") {
//     }
//
//     public DerivedType(int TestIntParameter) : 
//                this() {
//     }
// }

.NET Framework
Available since 1.1

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

Return to top
Show:
© 2016 Microsoft