다음을 통해 공유


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는 형식에 대한 instance 생성자의 선언을 나타내는 데 사용할 수 있습니다. 를 사용하여 CodeTypeConstructor 형식에 대한 정적 생성자를 선언합니다.

생성자가 기본 클래스 생성자를 호출하는 경우 속성의 기본 클래스 생성자에 대한 생성자 인수를 BaseConstructorArgs 설정합니다. 생성자가 형식에 대한 다른 생성자를 오버로드하는 경우 속성의 오버로드된 형식 생성자에 전달할 생성자 인수를 ChainedConstructorArgs 설정합니다.

생성자

CodeConstructor()

CodeConstructor 클래스의 새 인스턴스를 초기화합니다.

속성

Attributes

멤버의 특성을 가져오거나 설정합니다.

(다음에서 상속됨 CodeTypeMember)
BaseConstructorArgs

기본 생성자 인수의 컬렉션을 가져옵니다.

ChainedConstructorArgs

연결된 생성자 인수를 가져오거나 설정합니다.

Comments

형식 멤버의 주석 컬렉션을 가져옵니다.

(다음에서 상속됨 CodeTypeMember)
CustomAttributes

멤버의 사용자 지정 특성을 가져오거나 설정합니다.

(다음에서 상속됨 CodeTypeMember)
EndDirectives

멤버의 종료 지시문을 가져옵니다.

(다음에서 상속됨 CodeTypeMember)
ImplementationTypes

메서드가 PrivateImplementationType 속성에서 표시되는 프라이빗 메서드 구현이 아닌 경우 이 메서드에서 구현되는 인터페이스의 데이터 형식을 가져옵니다.

(다음에서 상속됨 CodeMemberMethod)
LinePragma

형식 멤버 문이 있는 줄을 가져오거나 설정합니다.

(다음에서 상속됨 CodeTypeMember)
Name

멤버의 이름을 가져오거나 설정합니다.

(다음에서 상속됨 CodeTypeMember)
Parameters

해당 메서드의 매개 변수 선언을 가져옵니다.

(다음에서 상속됨 CodeMemberMethod)
PrivateImplementationType

이 메서드의 데이터 형식이 프라이빗할 경우 이를 가져오거나 설정하고, 메서드가 있으면 이를 구현합니다.

(다음에서 상속됨 CodeMemberMethod)
ReturnType

메서드 반환 값의 데이터 형식을 가져오거나 설정합니다.

(다음에서 상속됨 CodeMemberMethod)
ReturnTypeCustomAttributes

메서드의 반환 형식에 대한 사용자 지정 특성을 가져옵니다.

(다음에서 상속됨 CodeMemberMethod)
StartDirectives

멤버의 시작 지시문을 가져옵니다.

(다음에서 상속됨 CodeTypeMember)
Statements

메서드의 문을 가져옵니다.

(다음에서 상속됨 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)

적용 대상