CodeConstructor CodeConstructor CodeConstructor CodeConstructor Class

定義

代表型別之執行個體建構函式的宣告。Represents a declaration for an instance constructor of a type.

public ref 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
繼承
屬性

範例

這個範例示範CodeConstructor如何使用來宣告數種類型的函式。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


備註

CodeConstructor可以用來表示類型之實例的宣告式宣告。CodeConstructor can be used to represent a declaration of an instance constructor for a type. 使用CodeTypeConstructor宣告類型的靜態函式。Use CodeTypeConstructor to declare a static constructor for a type.

如果此函式呼叫基類的函式, 請在BaseConstructorArgs屬性中設定基類的函式引數。If the constructor calls a base class constructor, set the constructor arguments for the base class constructor in the BaseConstructorArgs property. 如果此函式會多載類型的另一個函式, 請設定要傳遞至ChainedConstructorArgs屬性中多載型別的函式引數。If the constructor overloads another constructor for the type, set the constructor arguments to pass to the overloaded type constructor in the ChainedConstructorArgs property.

建構函式

CodeConstructor() CodeConstructor() CodeConstructor() CodeConstructor()

初始化 CodeConstructor 類別的新執行個體。Initializes a new instance of the CodeConstructor class.

屬性

Attributes Attributes Attributes Attributes

取得或設定成員的屬性 (Attribute)。Gets or sets the attributes of the member.

(Inherited from CodeTypeMember)
BaseConstructorArgs BaseConstructorArgs BaseConstructorArgs BaseConstructorArgs

取得基底建構函式引數的集合。Gets the collection of base constructor arguments.

ChainedConstructorArgs ChainedConstructorArgs ChainedConstructorArgs ChainedConstructorArgs

取得連鎖建構函式引數的集合。Gets the collection of chained constructor arguments.

Comments Comments Comments Comments

取得型別成員的註解集合。Gets the collection of comments for the type member.

(Inherited from CodeTypeMember)
CustomAttributes CustomAttributes CustomAttributes CustomAttributes

取得或設定成員的自訂屬性。Gets or sets the custom attributes of the member.

(Inherited from CodeTypeMember)
EndDirectives EndDirectives EndDirectives EndDirectives

取得成員的結尾指示詞。Gets the end directives for the member.

(Inherited from CodeTypeMember)
ImplementationTypes ImplementationTypes ImplementationTypes ImplementationTypes

取得這個方法所實作的介面的資料型別,除非它是 PrivateImplementationType 屬性所指示的私用 (Private) 方法實作。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 LinePragma LinePragma LinePragma

取得或設定型別成員陳述式 (Statement) 所在的行。Gets or sets the line on which the type member statement occurs.

(Inherited from CodeTypeMember)
Name Name Name Name

取得或設定成員的名稱。Gets or sets the name of the member.

(Inherited from CodeTypeMember)
Parameters Parameters Parameters Parameters

取得方法的參數宣告。Gets the parameter declarations for the method.

(Inherited from CodeMemberMethod)
PrivateImplementationType PrivateImplementationType PrivateImplementationType PrivateImplementationType

取得或設定這個方法之介面的資料型別 (如果是私用),實作其方法 (如果有的話)。Gets or sets the data type of the interface this method, if private, implements a method of, if any.

(Inherited from CodeMemberMethod)
ReturnType ReturnType ReturnType ReturnType

取得或設定方法之傳回值的資料型別。Gets or sets the data type of the return value of the method.

(Inherited from CodeMemberMethod)
ReturnTypeCustomAttributes ReturnTypeCustomAttributes ReturnTypeCustomAttributes ReturnTypeCustomAttributes

取得方法之傳回型別的自訂屬性 (Attribute)。Gets the custom attributes of the return type of the method.

(Inherited from CodeMemberMethod)
StartDirectives StartDirectives StartDirectives StartDirectives

取得成員的開頭指示詞。Gets the start directives for the member.

(Inherited from CodeTypeMember)
Statements Statements Statements Statements

取得方法中的陳述式 (Statement)。Gets the statements within the method.

(Inherited from CodeMemberMethod)
TypeParameters TypeParameters TypeParameters TypeParameters

取得目前泛型方法的型別參數。Gets the type parameters for the current generic method.

(Inherited from CodeMemberMethod)
UserData UserData UserData UserData

取得目前物件的使用者可定義資料。Gets the user-definable data for the current object.

(Inherited from CodeObject)

事件

PopulateImplementationTypes PopulateImplementationTypes PopulateImplementationTypes PopulateImplementationTypes

第一次存取 ImplementationTypes 集合時,將引發的事件。An event that will be raised the first time the ImplementationTypes collection is accessed.

(Inherited from CodeMemberMethod)
PopulateParameters PopulateParameters PopulateParameters PopulateParameters

第一次存取 Parameters 集合時,將引發的事件。An event that will be raised the first time the Parameters collection is accessed.

(Inherited from CodeMemberMethod)
PopulateStatements PopulateStatements PopulateStatements PopulateStatements

第一次存取 Statements 集合時,將引發的事件。An event that will be raised the first time the Statements collection is accessed.

(Inherited from CodeMemberMethod)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於