CodeConstructor Clase
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Representa una declaración para un constructor de instancia de un tipo.
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
- Herencia
- Atributos
Ejemplos
En este ejemplo se muestra el uso CodeConstructor de para declarar varios tipos de constructores.
// 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
Comentarios
CodeConstructor se puede usar para representar una declaración de un constructor de instancia para un tipo. Use CodeTypeConstructor para declarar un constructor estático para un tipo.
Si el constructor llama a un constructor de clase base, establezca los argumentos del constructor para el constructor de clase base en la BaseConstructorArgs propiedad . Si el constructor sobrecarga otro constructor para el tipo, establezca los argumentos del constructor para pasar al constructor de tipo sobrecargado en la ChainedConstructorArgs propiedad .
Constructores
| Nombre | Description |
|---|---|
| CodeConstructor() |
Inicializa una nueva instancia de la clase CodeConstructor. |
Propiedades
| Nombre | Description |
|---|---|
| Attributes |
Obtiene o establece los atributos del miembro. (Heredado de CodeTypeMember) |
| BaseConstructorArgs |
Obtiene la colección de argumentos de constructor base. |
| ChainedConstructorArgs |
Obtiene la colección de argumentos de constructor encadenados. |
| Comments |
Obtiene la colección de comentarios para el miembro de tipo. (Heredado de CodeTypeMember) |
| CustomAttributes |
Obtiene o establece los atributos personalizados del miembro. (Heredado de CodeTypeMember) |
| EndDirectives |
Obtiene las directivas end para el miembro. (Heredado de CodeTypeMember) |
| ImplementationTypes |
Obtiene los tipos de datos de las interfaces implementadas por este método, a menos que sea una implementación de método privado, que se indica mediante la PrivateImplementationType propiedad . (Heredado de CodeMemberMethod) |
| LinePragma |
Obtiene o establece la línea en la que se produce la instrucción miembro de tipo. (Heredado de CodeTypeMember) |
| Name |
Obtiene o establece el nombre del miembro. (Heredado de CodeTypeMember) |
| Parameters |
Obtiene las declaraciones de parámetro para el método . (Heredado de CodeMemberMethod) |
| PrivateImplementationType |
Obtiene o establece el tipo de datos de la interfaz de este método, si es privado, implementa un método de , si existe. (Heredado de CodeMemberMethod) |
| ReturnType |
Obtiene o establece el tipo de datos del valor devuelto del método . (Heredado de CodeMemberMethod) |
| ReturnTypeCustomAttributes |
Obtiene los atributos personalizados del tipo de valor devuelto del método . (Heredado de CodeMemberMethod) |
| StartDirectives |
Obtiene las directivas start para el miembro. (Heredado de CodeTypeMember) |
| Statements |
Obtiene las instrucciones dentro del método . (Heredado de CodeMemberMethod) |
| TypeParameters |
Obtiene los parámetros de tipo para el método genérico actual. (Heredado de CodeMemberMethod) |
| UserData |
Obtiene los datos definibles por el usuario para el objeto actual. (Heredado de CodeObject) |
Métodos
| Nombre | Description |
|---|---|
| Equals(Object) |
Determina si el objeto especificado es igual al objeto actual. (Heredado de Object) |
| GetHashCode() |
Actúa como función hash predeterminada. (Heredado de Object) |
| GetType() |
Obtiene el Type de la instancia actual. (Heredado de Object) |
| MemberwiseClone() |
Crea una copia superficial del Objectactual. (Heredado de Object) |
| ToString() |
Devuelve una cadena que representa el objeto actual. (Heredado de Object) |
Eventos
| Nombre | Description |
|---|---|
| PopulateImplementationTypes |
Evento que se generará la primera vez que se obtiene acceso a la ImplementationTypes colección. (Heredado de CodeMemberMethod) |
| PopulateParameters |
Evento que se generará la primera vez que se obtiene acceso a la Parameters colección. (Heredado de CodeMemberMethod) |
| PopulateStatements |
Evento que se generará la primera vez que se obtiene acceso a la Statements colección. (Heredado de CodeMemberMethod) |