CodeConstructor Class

Definition

Stellt eine Deklaration eines Instanzkonstruktors für einen Typ dar.Represents a declaration for an instance constructor of a type.

public ref class CodeConstructor : System::CodeDom::CodeMemberMethod
public class CodeConstructor : System.CodeDom.CodeMemberMethod
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Serializable]
public class CodeConstructor : System.CodeDom.CodeMemberMethod
type CodeConstructor = class
    inherit CodeMemberMethod
Public Class CodeConstructor
Inherits CodeMemberMethod
Inheritance
Attributes

Examples

Dieses Beispiel veranschaulicht die Verwendung eines CodeConstructor, um verschiedene Typen von Konstruktoren zu deklarieren.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 = gcnew CodeCompileUnit;

// Declares a new namespace object and names it.
CodeNamespace^ Samples = gcnew 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( gcnew CodeNamespaceImport( "System" ) );

// Declares a new type and names it.
CodeTypeDeclaration^ BaseType = gcnew 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 = gcnew 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 = gcnew CodeConstructor;
stringConstructor->Attributes = MemberAttributes::Public;

// Declares a parameter of type string named "TestStringParameter".
stringConstructor->Parameters->Add( gcnew 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 = gcnew CodeTypeDeclaration( "DerivedType" );

// The DerivedType class inherits from the BaseType class.
DerivedType->BaseTypes->Add( gcnew 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 = gcnew CodeConstructor;
baseStringConstructor->Attributes = MemberAttributes::Public;

// Declares a parameter of type string named "TestStringParameter".    
baseStringConstructor->Parameters->Add( gcnew CodeParameterDeclarationExpression( "System.String","TestStringParameter" ) );

// Calls a base class constructor with the TestStringParameter parameter.
baseStringConstructor->BaseConstructorArgs->Add( gcnew 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 = gcnew CodeConstructor;
overloadConstructor->Attributes = MemberAttributes::Public;

// Sets the argument to pass to a base constructor method.
overloadConstructor->ChainedConstructorArgs->Add( gcnew 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 = gcnew CodeConstructor;
overloadConstructor2->Attributes = MemberAttributes::Public;
overloadConstructor2->Parameters->Add( gcnew CodeParameterDeclarationExpression( "System.Int32","TestIntParameter" ) );

// Sets the argument to pass to a base constructor method.
overloadConstructor2->ChainedConstructorArgs->Add( gcnew 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.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.
 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


Remarks

CodeConstructor kann verwendet werden, um eine Deklaration eines Instanzkonstruktors für einen Typ darzustellen.CodeConstructor can be used to represent a declaration of an instance constructor for a type. Verwenden Sie CodeTypeConstructor, um einen statischen Konstruktor für einen Typ zu deklarieren.Use CodeTypeConstructor to declare a static constructor for a type.

Wenn der Konstruktor einen Basisklassenkonstruktor aufruft, legen Sie die Konstruktorargumente für den Basisklassenkonstruktor in der BaseConstructorArgs-Eigenschaft fest.If the constructor calls a base class constructor, set the constructor arguments for the base class constructor in the BaseConstructorArgs property. Wenn der Konstruktor einen anderen Konstruktor für den Typ überlädt, legen Sie die Konstruktorargumente so fest, dass Sie in der ChainedConstructorArgs-Eigenschaft an den überladenen Typkonstruktor übergeben werden.If the constructor overloads another constructor for the type, set the constructor arguments to pass to the overloaded type constructor in the ChainedConstructorArgs property.

Constructors

CodeConstructor()

Initialisiert eine neue Instanz der CodeConstructor-Klasse.Initializes a new instance of the CodeConstructor class.

Properties

Attributes

Ruft die Attribute des Elements ab oder legt sie fest.Gets or sets the attributes of the member.

(Inherited from CodeTypeMember)
BaseConstructorArgs

Ruft die Auflistung von Basiskonstruktorargumenten ab.Gets the collection of base constructor arguments.

ChainedConstructorArgs

Ruft die Auflistung von verketteten Konstruktorargumenten ab.Gets the collection of chained constructor arguments.

Comments

Ruft die Auflistung der Kommentare für den Typmember ab.Gets the collection of comments for the type member.

(Inherited from CodeTypeMember)
CustomAttributes

Ruft die benutzerdefinierten Attribute des Members ab oder legt diese fest.Gets or sets the custom attributes of the member.

(Inherited from CodeTypeMember)
EndDirectives

Ruft die Enddirektiven für den Member ab.Gets the end directives for the member.

(Inherited from CodeTypeMember)
ImplementationTypes

Ruft die Datentypen der von dieser Methode implementierten Schnittstellen ab, sofern es sich nicht um die Implementierung einer privaten Methode handelt. Dies wird durch die PrivateImplementationType-Eigenschaft angegeben.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)
LinePragma

Ruft die Zeile ab oder oder legt die Zeile fest, auf der die Typmemberanweisung auftritt.Gets or sets the line on which the type member statement occurs.

(Inherited from CodeTypeMember)
Name

Ruft den Namen des Members ab oder legt diesen fest.Gets or sets the name of the member.

(Inherited from CodeTypeMember)
Parameters

Ruft die Parameterdeklarationen für die Methode ab.Gets the parameter declarations for the method.

(Inherited from CodeMemberMethod)
PrivateImplementationType

Ruft den Datentyp der Schnittstelle ab, für die diese Methode (sofern es sich um eine private Methode handelt) ggf. eine Methode implementiert, oder legt diesen fest.Gets or sets the data type of the interface this method, if private, implements a method of, if any.

(Inherited from CodeMemberMethod)
ReturnType

Ruft den Datentyp des Rückgabewerts der Methode ab oder legt diesen fest.Gets or sets the data type of the return value of the method.

(Inherited from CodeMemberMethod)
ReturnTypeCustomAttributes

Ruft die benutzerdefinierten Attribute des Rückgabetyps der Methode ab.Gets the custom attributes of the return type of the method.

(Inherited from CodeMemberMethod)
StartDirectives

Ruft die Startdirektiven für den Member ab.Gets the start directives for the member.

(Inherited from CodeTypeMember)
Statements

Ruft die Anweisungen in der Methode ab.Gets the statements within the method.

(Inherited from CodeMemberMethod)
TypeParameters

Ruft die Typparameter für die aktuelle generische Methode ab.Gets the type parameters for the current generic method.

(Inherited from CodeMemberMethod)
UserData

Ruft die benutzerdefinierbaren Daten für das aktuelle Objekt ab.Gets the user-definable data for the current object.

(Inherited from CodeObject)

Methods

Equals(Object)

Bestimmt, ob das angegebene Objekt mit dem aktuellen Objekt identisch ist.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Fungiert als Standardhashfunktion.Serves as the default hash function.

(Inherited from Object)
GetType()

Ruft den Type der aktuellen Instanz ab.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.Returns a string that represents the current object.

(Inherited from Object)

Events

PopulateImplementationTypes

Ein Ereignis, das beim ersten Zugriff auf die ImplementationTypes-Auflistung ausgelöst wird.An event that will be raised the first time the ImplementationTypes collection is accessed.

(Inherited from CodeMemberMethod)
PopulateParameters

Ein Ereignis, das beim ersten Zugriff auf die Parameters-Auflistung ausgelöst wird.An event that will be raised the first time the Parameters collection is accessed.

(Inherited from CodeMemberMethod)
PopulateStatements

Ein Ereignis, das beim ersten Zugriff auf die Statements-Auflistung ausgelöst wird.An event that will be raised the first time the Statements collection is accessed.

(Inherited from CodeMemberMethod)

Applies to