CodeConstructor クラス

定義

型のインスタンス コンストラクターの宣言を表します。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 クラスの新しいインスタンスを初期化します。Initializes a new instance of the CodeConstructor class.

プロパティ

Attributes

メンバーの属性を取得または設定します。Gets or sets the attributes of the member.

(継承元 CodeTypeMember)
BaseConstructorArgs

基本コンストラクターの引数のコレクションを取得します。Gets the collection of base constructor arguments.

ChainedConstructorArgs

チェイン コンストラクターの引数のコレクションを取得します。Gets the collection of chained constructor arguments.

Comments

型メンバーのコメント コレクションを取得します。Gets the collection of comments for the type member.

(継承元 CodeTypeMember)
CustomAttributes

メンバーのカスタム属性を取得または設定します。Gets or sets the custom attributes of the member.

(継承元 CodeTypeMember)
EndDirectives

メンバーの終了ディレクティブを取得します。Gets the end directives for the member.

(継承元 CodeTypeMember)
ImplementationTypes

メソッドの実装が PrivateImplementationType プロパティで示されるプライベート メソッド実装である場合を除き、このメソッドによって実装されるインターフェイスのデータ型を取得します。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.

(継承元 CodeMemberMethod)
LinePragma

型メンバーのステートメントが発生する行を取得または設定します。Gets or sets the line on which the type member statement occurs.

(継承元 CodeTypeMember)
Name

メンバーの名前を取得または設定します。Gets or sets the name of the member.

(継承元 CodeTypeMember)
Parameters

メソッドのパラメーター宣言を取得します。Gets the parameter declarations for the method.

(継承元 CodeMemberMethod)
PrivateImplementationType

このメソッドによってプライベート メソッドが実装されるインターフェイスがある場合に、そのインターフェイスのデータ型を取得または設定します。Gets or sets the data type of the interface this method, if private, implements a method of, if any.

(継承元 CodeMemberMethod)
ReturnType

メソッドの戻り値のデータ型を取得または設定します。Gets or sets the data type of the return value of the method.

(継承元 CodeMemberMethod)
ReturnTypeCustomAttributes

メソッドの戻り値の型のカスタム属性を取得します。Gets the custom attributes of the return type of the method.

(継承元 CodeMemberMethod)
StartDirectives

メンバーの開始ディレクティブを取得します。Gets the start directives for the member.

(継承元 CodeTypeMember)
Statements

メソッド内で、ステートメントを取得します。Gets the statements within the method.

(継承元 CodeMemberMethod)
TypeParameters

現在のジェネリック メソッドの型パラメーターを取得します。Gets the type parameters for the current generic method.

(継承元 CodeMemberMethod)
UserData

現在のオブジェクトのユーザー定義可能なデータを取得します。Gets the user-definable data for the current object.

(継承元 CodeObject)

メソッド

Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判定します。Determines whether the specified object is equal to the current object.

(継承元 Object)
GetHashCode()

既定のハッシュ関数として機能します。Serves as the default hash function.

(継承元 Object)
GetType()

現在のインスタンスの Type を取得します。Gets the Type of the current instance.

(継承元 Object)
MemberwiseClone()

現在の Object の簡易コピーを作成します。Creates a shallow copy of the current Object.

(継承元 Object)
ToString()

現在のオブジェクトを表す string を返します。Returns a string that represents the current object.

(継承元 Object)

イベント

PopulateImplementationTypes

ImplementationTypes コレクションが最初にアクセスされたときに発生するイベント。An event that will be raised the first time the ImplementationTypes collection is accessed.

(継承元 CodeMemberMethod)
PopulateParameters

Parameters コレクションが最初にアクセスされたときに発生するイベント。An event that will be raised the first time the Parameters collection is accessed.

(継承元 CodeMemberMethod)
PopulateStatements

Statements コレクションが最初にアクセスされたときに発生するイベント。An event that will be raised the first time the Statements collection is accessed.

(継承元 CodeMemberMethod)

適用対象