Export (0) Print
Expand All

CodeConstructor Class

Represents a declaration for an instance constructor of a type.

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

[SerializableAttribute] 
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class CodeConstructor : CodeMemberMethod
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class CodeConstructor extends CodeMemberMethod
SerializableAttribute 
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class CodeConstructor extends CodeMemberMethod
Not applicable.

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() {
//     }
// }


// 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.get_Namespaces().Add(samples);

// Adds a new namespace import for the System namespace.
samples.get_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.get_Types().Add(baseType);

// Declares a default constructor that takes no arguments.
CodeConstructor defaultConstructor = new CodeConstructor();

defaultConstructor.set_Attributes(MemberAttributes.Public);

// Adds the constructor to the Members collection of the BaseType.
baseType.get_Members().Add(defaultConstructor);

// Declares a constructor that takes a string argument.
CodeConstructor stringConstructor = new CodeConstructor();

stringConstructor.set_Attributes(MemberAttributes.Public);

// Declares a parameter of type string named "TestStringParameter".
stringConstructor.get_Parameters().Add(
    new CodeParameterDeclarationExpression("System.String",
    "TestStringParameter"));

// Adds the constructor to the Members collection of the BaseType.
baseType.get_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.get_BaseTypes().Add(new CodeTypeReference("BaseType"));

// Adds the new type to the namespace object's type collection.
samples.get_Types().Add(derivedType);

// Declare a constructor that takes a string argument and calls the 
// base class constructor with it.
CodeConstructor baseStringConstructor = new CodeConstructor();

baseStringConstructor.set_Attributes(MemberAttributes.Public);

// Declares a parameter of type string named "TestStringParameter".    
baseStringConstructor.get_Parameters().Add(
    new CodeParameterDeclarationExpression("System.String",
    "TestStringParameter"));

// Calls a base class constructor with the TestStringParameter parameter.
baseStringConstructor.get_BaseConstructorArgs().Add(
    new CodeVariableReferenceExpression("TestStringParameter"));

// Adds the constructor to the Members collection of the DerivedType.
derivedType.get_Members().Add(baseStringConstructor);

// Declares a constructor overload that calls another constructor for
// the type with a predefined argument.
CodeConstructor overloadConstructor = new CodeConstructor();

overloadConstructor.set_Attributes(MemberAttributes.Public);

// Sets the argument to pass to a base constructor method.
overloadConstructor.get_ChainedConstructorArgs().Add(
    new CodePrimitiveExpression("Test"));

// Adds the constructor to the Members collection of the DerivedType.
derivedType.get_Members().Add(overloadConstructor);

// Declares a constructor overload that calls the default constructor
// for the type.
CodeConstructor overloadConstructor2 = new CodeConstructor();

overloadConstructor2.set_Attributes(MemberAttributes.Public);
overloadConstructor2.get_Parameters().Add(
    new CodeParameterDeclarationExpression("System.Int32",
    "TestIntParameter"));

// Sets the argument to pass to a base constructor method.
overloadConstructor2.get_ChainedConstructorArgs().Add(
    new CodeSnippetExpression(""));

// Adds the constructor to the Members collection of the DerivedType.
derivedType.get_Members().Add(overloadConstructor2);
// A VJ# code generator produces the following source code for the
// preceeding
// example code:
// public class BaseType {
//     
//     public BaseType() {
//     }
//        
//     public BaseType(string TestStringParameter) {
//     }
// }
//    
// public class DerivedType extands BaseType {
//        
//     public DerivedType(string TestStringParameter)
//       {
//             super(TestStringParameter) 
//     }
//        
//     public DerivedType()
//     {
//             DerivedType("Test") 
//     }
//
//     public DerivedType(int TestIntParameter)
//     {
//                DerivedType() 
//     }
// }

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

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

Community Additions

ADD
Show:
© 2014 Microsoft