CodeConstructor 類別

定義

代表型別之執行個體建構函式的宣告。

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

範例

此範例示範如何使用 CodeConstructor 來宣告數種類型的建構函式。

// 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 可用來表示型別之實例建構函式的宣告。 使用 CodeTypeConstructor 宣告類型的靜態建構函式。

如果建構函式呼叫基類建構函式,請在 屬性中設定基類建構函式的 BaseConstructorArgs 建構函式自變數。 如果建構函式多載類型的另一個建構函式,請將建構函式自變數設定為傳遞至 屬性中的 ChainedConstructorArgs 多載型別建構函式。

建構函式

CodeConstructor()

初始化 CodeConstructor 類別的新執行個體。

屬性

Attributes

取得或設定成員的屬性 (Attribute)。

(繼承來源 CodeTypeMember)
BaseConstructorArgs

取得基底建構函式引數的集合。

ChainedConstructorArgs

取得連鎖建構函式引數的集合。

Comments

取得型別成員的註解集合。

(繼承來源 CodeTypeMember)
CustomAttributes

取得或設定成員的自訂屬性。

(繼承來源 CodeTypeMember)
EndDirectives

取得成員的結尾指示詞。

(繼承來源 CodeTypeMember)
ImplementationTypes

取得這個方法所實作的介面的資料型別,除非它是 PrivateImplementationType 屬性所指示的私用 (Private) 方法實作。

(繼承來源 CodeMemberMethod)
LinePragma

取得或設定型別成員陳述式 (Statement) 所在的行。

(繼承來源 CodeTypeMember)
Name

取得或設定成員的名稱。

(繼承來源 CodeTypeMember)
Parameters

取得方法的參數宣告。

(繼承來源 CodeMemberMethod)
PrivateImplementationType

取得或設定這個方法之介面的資料型別 (如果是私用),實作其方法 (如果有的話)。

(繼承來源 CodeMemberMethod)
ReturnType

取得或設定方法之傳回值的資料型別。

(繼承來源 CodeMemberMethod)
ReturnTypeCustomAttributes

取得方法之傳回型別的自訂屬性 (Attribute)。

(繼承來源 CodeMemberMethod)
StartDirectives

取得成員的開頭指示詞。

(繼承來源 CodeTypeMember)
Statements

取得方法中的陳述式 (Statement)。

(繼承來源 CodeMemberMethod)
TypeParameters

取得目前泛型方法的型別參數。

(繼承來源 CodeMemberMethod)
UserData

取得目前物件的使用者可定義資料。

(繼承來源 CodeObject)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

事件

PopulateImplementationTypes

第一次存取 ImplementationTypes 集合時,將引發的事件。

(繼承來源 CodeMemberMethod)
PopulateParameters

第一次存取 Parameters 集合時,將引發的事件。

(繼承來源 CodeMemberMethod)
PopulateStatements

第一次存取 Statements 集合時,將引發的事件。

(繼承來源 CodeMemberMethod)

適用於