CodeConstructor Klas
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Vertegenwoordigt een declaratie voor een exemplaarconstructor van een type.
public ref 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
[<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
- Overname
- Kenmerken
Voorbeelden
In dit voorbeeld ziet u hoe u een CodeConstructor aantal typen constructors kunt declareren.
// 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
Opmerkingen
CodeConstructor kan worden gebruikt om een declaratie van een exemplaarconstructor voor een type weer te geven. Gebruik CodeTypeConstructor dit om een statische constructor voor een type te declareren.
Als de constructor een basisklasseconstructor aanroept, stelt u de constructorargumenten in voor de basisklasseconstructor in de BaseConstructorArgs eigenschap. Als de constructor een andere constructor voor het type overbelast, stelt u de constructorargumenten in die moeten worden doorgegeven aan de overbelaste typeconstructor in de ChainedConstructorArgs eigenschap.
Constructors
| Name | Description |
|---|---|
| CodeConstructor() |
Initialiseert een nieuw exemplaar van de CodeConstructor klasse. |
Eigenschappen
| Name | Description |
|---|---|
| Attributes |
Hiermee worden de kenmerken van het lid opgehaald of ingesteld. (Overgenomen van CodeTypeMember) |
| BaseConstructorArgs |
Hiermee haalt u de verzameling basisconstructorargumenten op. |
| ChainedConstructorArgs |
Hiermee haalt u de verzameling ketenconstructorargumenten op. |
| Comments |
Hiermee haalt u de verzameling opmerkingen voor het typelid op. (Overgenomen van CodeTypeMember) |
| CustomAttributes |
Hiermee worden de aangepaste kenmerken van het lid opgehaald of ingesteld. (Overgenomen van CodeTypeMember) |
| EndDirectives |
Hiermee haalt u de eindrichtlijnen voor het lid op. (Overgenomen van CodeTypeMember) |
| ImplementationTypes |
Hiermee haalt u de gegevenstypen op van de interfaces die door deze methode zijn geïmplementeerd, tenzij het een implementatie van een privémethode is, die wordt aangegeven door de PrivateImplementationType eigenschap. (Overgenomen van CodeMemberMethod) |
| LinePragma |
Hiermee haalt u de regel op waarop de lidinstructie van het type plaatsvindt of stelt u deze in. (Overgenomen van CodeTypeMember) |
| Name |
Hiermee haalt u de naam van het lid op of stelt u deze in. (Overgenomen van CodeTypeMember) |
| Parameters |
Hiermee haalt u de parameterdeclaraties voor de methode op. (Overgenomen van CodeMemberMethod) |
| PrivateImplementationType |
Hiermee haalt u het gegevenstype van de interface op of stelt u deze methode in, indien privé, een methode van, indien van toepassing, implementeert. (Overgenomen van CodeMemberMethod) |
| ReturnType |
Hiermee haalt u het gegevenstype van de retourwaarde van de methode op of stelt u deze in. (Overgenomen van CodeMemberMethod) |
| ReturnTypeCustomAttributes |
Hiermee haalt u de aangepaste kenmerken van het retourtype van de methode op. (Overgenomen van CodeMemberMethod) |
| StartDirectives |
Hiermee haalt u de beginrichtlijnen voor het lid op. (Overgenomen van CodeTypeMember) |
| Statements |
Haalt de instructies in de methode op. (Overgenomen van CodeMemberMethod) |
| TypeParameters |
Hiermee haalt u de typeparameters voor de huidige algemene methode op. (Overgenomen van CodeMemberMethod) |
| UserData |
Haalt de door de gebruiker gedefinieerde gegevens voor het huidige object op. (Overgenomen van CodeObject) |
Methoden
| Name | Description |
|---|---|
| Equals(Object) |
Bepaalt of het opgegeven object gelijk is aan het huidige object. (Overgenomen van Object) |
| GetHashCode() |
Fungeert als de standaardhashfunctie. (Overgenomen van Object) |
| GetType() |
Hiermee haalt u de Type huidige instantie op. (Overgenomen van Object) |
| MemberwiseClone() |
Hiermee maakt u een ondiepe kopie van de huidige Object. (Overgenomen van Object) |
| ToString() |
Retourneert een tekenreeks die het huidige object vertegenwoordigt. (Overgenomen van Object) |
gebeurtenis
| Name | Description |
|---|---|
| PopulateImplementationTypes |
Een gebeurtenis die wordt gegenereerd wanneer de verzameling voor het ImplementationTypes eerst wordt geopend. (Overgenomen van CodeMemberMethod) |
| PopulateParameters |
Een gebeurtenis die wordt gegenereerd wanneer de verzameling voor het Parameters eerst wordt geopend. (Overgenomen van CodeMemberMethod) |
| PopulateStatements |
Een gebeurtenis die wordt gegenereerd wanneer de verzameling voor het Statements eerst wordt geopend. (Overgenomen van CodeMemberMethod) |