Export (0) Print
Expand All

CodeConstructor Class

Represents a declaration for an instance constructor of a type.

For a list of all members of this type, see CodeConstructor Members.

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

[Visual Basic]
<Serializable>
<ClassInterface(ClassInterfaceType.AutoDispatch)>
<ComVisible(True)>
Public Class CodeConstructor
   Inherits CodeMemberMethod
[C#]
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ComVisible(true)]
public class CodeConstructor : CodeMemberMethod
[C++]
[Serializable]
[ClassInterface(ClassInterfaceType::AutoDispatch)]
[ComVisible(true)]
public __gc class CodeConstructor : public CodeMemberMethod
[JScript]
public
   Serializable
 ClassInterface(ClassInterfaceType.AutoDispatch)
 ComVisible(true)
class CodeConstructor extends CodeMemberMethod

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.

Remarks

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.

Example

[Visual Basic, C#, C++] This example demonstrates using a CodeConstructor to declare several types of constructors.

[Visual Basic] 
' 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.
Dim CompileUnit As New CodeCompileUnit()
' Declares a new namespace object and names it.
Dim Samples As 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.
Dim BaseType As 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.
Dim defaultConstructor As 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.
Dim stringConstructor As 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.
Dim DerivedType As 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.
Dim baseStringConstructor As 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.
Dim overloadConstructor As 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 another constructor for the type.
Dim overloadConstructor2 As 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 Visual Basic code generator produces the following source code for the preceeding example code:
        
        ' Public Class BaseType
        '     
        '     Public Sub New()
        '         MyBase.New
        '     End Sub
        '        
        '     Public Sub New(ByVal TestStringParameter As String)
        '         MyBase.New
        '     End Sub
        ' End Class
        '    
        ' Public Class DerivedType
        '     Inherits BaseType
        '        
        '     Public Sub New(ByVal TestStringParameter As String)
        '         MyBase.New(TestStringParameter)
        '     End Sub
        '     
        '     Public Sub New()
        '         Me.New("Test")
        '     End Sub
        '
        '     Public Sub New(ByVal TestIntParameter As Integer)
        '         Me.New()
        '     End Sub
        ' End Class



[C#] 
// 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() {
//     }
// }


[C++] 
// 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(S"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(S"System") );            

// Declares a new type and names it.
CodeTypeDeclaration* BaseType = new CodeTypeDeclaration(S"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(S"System.String", S"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(S"DerivedType");
// The DerivedType class inherits from the BaseType class.
DerivedType->BaseTypes->Add( new CodeTypeReference(S"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(S"System.String", S"TestStringParameter") );
// Calls a base class constructor with the TestStringParameter parameter.
baseStringConstructor->BaseConstructorArgs->Add( new CodeVariableReferenceExpression(S"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(S"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(S"System.Int32", S"TestIntParameter") );
// Sets the argument to pass to a base constructor method.
overloadConstructor2->ChainedConstructorArgs->Add( new CodeSnippetExpression(S"") );
// 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() {
//     }
// }

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.CodeDom

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

CodeConstructor Members | System.CodeDom Namespace

Show:
© 2014 Microsoft