Freigeben über


CodeConstructor Klasse

Definition

Stellt eine Deklaration für einen Instanzkonstruktor eines Typs dar.

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

Beispiele

In diesem Beispiel wird die Verwendung mehrerer CodeConstructor Konstruktorentypen veranschaulicht.

// 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

Hinweise

CodeConstructor kann verwendet werden, um eine Deklaration eines Instanzkonstruktors für einen Typ darzustellen. Dient CodeTypeConstructor zum Deklarieren eines statischen Konstruktors für einen Typ.

Wenn der Konstruktor einen Basisklassenkonstruktor aufruft, legen Sie die Konstruktorargumente für den Basisklassenkonstruktor in der BaseConstructorArgs Eigenschaft fest. Wenn der Konstruktor einen anderen Konstruktor für den Typ überlastet, legen Sie die Konstruktorargumente fest, die an den überladenen Typkonstruktor in der ChainedConstructorArgs Eigenschaft übergeben werden sollen.

Konstruktoren

Name Beschreibung
CodeConstructor()

Initialisiert eine neue Instanz der CodeConstructor-Klasse.

Eigenschaften

Name Beschreibung
Attributes

Ruft die Attribute des Elements ab oder legt diese fest.

(Geerbt von CodeTypeMember)
BaseConstructorArgs

Ruft die Auflistung der Basiskonstruktorargumente ab.

ChainedConstructorArgs

Ruft die Auflistung der verketteten Konstruktorargumente ab.

Comments

Ruft die Sammlung von Kommentaren für das Typelement ab.

(Geerbt von CodeTypeMember)
CustomAttributes

Dient zum Abrufen oder Festlegen der benutzerdefinierten Attribute des Elements.

(Geerbt von CodeTypeMember)
EndDirectives

Ruft die Enddirektiven für das Element ab.

(Geerbt von CodeTypeMember)
ImplementationTypes

Ruft die Datentypen der von dieser Methode implementierten Schnittstellen ab, es sei denn, es handelt sich um eine private Methodenimplementierung, die durch die PrivateImplementationType Eigenschaft angegeben wird.

(Geerbt von CodeMemberMethod)
LinePragma

Ruft die Zeile ab, in der die Element-Anweisung des Typs auftritt, oder legt sie fest.

(Geerbt von CodeTypeMember)
Name

Dient zum Abrufen oder Festlegen des Namens des Elements.

(Geerbt von CodeTypeMember)
Parameters

Ruft die Parameterdeklarationen für die Methode ab.

(Geerbt von CodeMemberMethod)
PrivateImplementationType

Dient zum Abrufen oder Festlegen des Datentyps der Schnittstelle, die diese Methode, falls privat, eine Methode von, falls vorhanden, implementiert.

(Geerbt von CodeMemberMethod)
ReturnType

Dient zum Abrufen oder Festlegen des Datentyps des Rückgabewerts der Methode.

(Geerbt von CodeMemberMethod)
ReturnTypeCustomAttributes

Ruft die benutzerdefinierten Attribute des Rückgabetyps der Methode ab.

(Geerbt von CodeMemberMethod)
StartDirectives

Ruft die Startdirektiven für das Mitglied ab.

(Geerbt von CodeTypeMember)
Statements

Ruft die Anweisungen innerhalb der Methode ab.

(Geerbt von CodeMemberMethod)
TypeParameters

Ruft die Typparameter für die aktuelle generische Methode ab.

(Geerbt von CodeMemberMethod)
UserData

Ruft die vom Benutzer definierbaren Daten für das aktuelle Objekt ab.

(Geerbt von CodeObject)

Methoden

Name Beschreibung
Equals(Object)

Bestimmt, ob das angegebene Objekt dem aktuellen Objekt entspricht.

(Geerbt von Object)
GetHashCode()

Dient als Standardhashfunktion.

(Geerbt von Object)
GetType()

Ruft die Type der aktuellen Instanz ab.

(Geerbt von Object)
MemberwiseClone()

Erstellt eine flache Kopie der aktuellen Object.

(Geerbt von Object)
ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.

(Geerbt von Object)

Ereignisse

Name Beschreibung
PopulateImplementationTypes

Ein Ereignis, das beim ersten Zugriff auf die ImplementationTypes Auflistung ausgelöst wird.

(Geerbt von CodeMemberMethod)
PopulateParameters

Ein Ereignis, das beim ersten Zugriff auf die Parameters Auflistung ausgelöst wird.

(Geerbt von CodeMemberMethod)
PopulateStatements

Ein Ereignis, das beim ersten Zugriff auf die Statements Auflistung ausgelöst wird.

(Geerbt von CodeMemberMethod)

Gilt für: