CodeCompileUnit CodeCompileUnit CodeCompileUnit CodeCompileUnit Class

Definición

Proporciona un contenedor para un gráfico de programa CodeDOM.Provides a container for a CodeDOM program graph.

public ref class CodeCompileUnit : System::CodeDom::CodeObject
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Serializable]
public class CodeCompileUnit : System.CodeDom.CodeObject
type CodeCompileUnit = class
    inherit CodeObject
Public Class CodeCompileUnit
Inherits CodeObject
Herencia
CodeCompileUnitCodeCompileUnitCodeCompileUnitCodeCompileUnit
Derivado
Atributos

Ejemplos

En el ejemplo de código siguiente se CodeCompileUnit crea un que modela la estructura del programa de un sencillo programa "Hola mundo".The following code example constructs a CodeCompileUnit that models the program structure of a simple "Hello World" program. Este ejemplo de código forma parte de un ejemplo más grande que también genera código a partir de este modelo, y CodeDomProvider se proporciona para la clase.This code example is part of a larger example that also produces code from this model, and is provided for the CodeDomProvider class.

// Build a Hello World program graph using 
// System::CodeDom types.
static CodeCompileUnit^ BuildHelloWorldGraph()
{
    // Create a new CodeCompileUnit to contain 
    // the program graph.
    CodeCompileUnit^ compileUnit = gcnew CodeCompileUnit;

    // Declare a new namespace called Samples.
    CodeNamespace^ samples = gcnew CodeNamespace( "Samples" );

    // Add the new namespace to the compile unit.
    compileUnit->Namespaces->Add( samples );

    // Add the new namespace import for the System namespace.
    samples->Imports->Add( gcnew CodeNamespaceImport( "System" ) );

    // Declare a new type called Class1.
    CodeTypeDeclaration^ class1 = gcnew CodeTypeDeclaration( "Class1" );

    // Add the new type to the namespace's type collection.
    samples->Types->Add( class1 );

    // Declare a new code entry point method.
    CodeEntryPointMethod^ start = gcnew CodeEntryPointMethod;

    // Create a type reference for the System::Console class.
    CodeTypeReferenceExpression^ csSystemConsoleType = gcnew CodeTypeReferenceExpression( "System.Console" );

    // Build a Console::WriteLine statement.
    CodeMethodInvokeExpression^ cs1 = gcnew CodeMethodInvokeExpression( csSystemConsoleType,"WriteLine", gcnew CodePrimitiveExpression("Hello World!") );

    // Add the WriteLine call to the statement collection.
    start->Statements->Add( cs1 );

    // Build another Console::WriteLine statement.
    CodeMethodInvokeExpression^ cs2 = gcnew CodeMethodInvokeExpression( csSystemConsoleType,"WriteLine", gcnew CodePrimitiveExpression( "Press the Enter key to continue." ) );

    // Add the WriteLine call to the statement collection.
    start->Statements->Add( cs2 );

    // Build a call to System::Console::ReadLine.
    CodeMethodReferenceExpression^ csReadLine = gcnew CodeMethodReferenceExpression( csSystemConsoleType, "ReadLine" );
    CodeMethodInvokeExpression^ cs3 = gcnew CodeMethodInvokeExpression( csReadLine, gcnew array<CodeExpression^>(0) );

    // Add the ReadLine statement.
    start->Statements->Add( cs3 );

    // Add the code entry point method to
    // the Members collection of the type.
    class1->Members->Add( start );
    return compileUnit;
}
// Build a Hello World program graph using 
// System.CodeDom types.
public static CodeCompileUnit BuildHelloWorldGraph()
{
    // Create a new CodeCompileUnit to contain 
    // the program graph.
    CodeCompileUnit compileUnit = new CodeCompileUnit();

    // Declare a new namespace called Samples.
    CodeNamespace samples = new CodeNamespace("Samples");
    // Add the new namespace to the compile unit.
    compileUnit.Namespaces.Add(samples);

    // Add the new namespace import for the System namespace.
    samples.Imports.Add(new CodeNamespaceImport("System"));

    // Declare a new type called Class1.
    CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1");
    // Add the new type to the namespace type collection.
    samples.Types.Add(class1);

    // Declare a new code entry point method.
    CodeEntryPointMethod start = new CodeEntryPointMethod();

    // Create a type reference for the System.Console class.
    CodeTypeReferenceExpression csSystemConsoleType = new CodeTypeReferenceExpression("System.Console");

    // Build a Console.WriteLine statement.
    CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(
        csSystemConsoleType, "WriteLine",
        new CodePrimitiveExpression("Hello World!"));

    // Add the WriteLine call to the statement collection.
    start.Statements.Add(cs1);

    // Build another Console.WriteLine statement.
    CodeMethodInvokeExpression cs2 = new CodeMethodInvokeExpression(
        csSystemConsoleType, "WriteLine",
        new CodePrimitiveExpression("Press the Enter key to continue."));

    // Add the WriteLine call to the statement collection.
    start.Statements.Add(cs2);

    // Build a call to System.Console.ReadLine.
    CodeMethodInvokeExpression csReadLine = new CodeMethodInvokeExpression(
        csSystemConsoleType, "ReadLine");

    // Add the ReadLine statement.
    start.Statements.Add(csReadLine);

    // Add the code entry point method to
    // the Members collection of the type.
    class1.Members.Add(start);

    return compileUnit;
}
' Build a Hello World program graph using 
' System.CodeDom types.
Public Shared Function BuildHelloWorldGraph() As CodeCompileUnit

    ' Create a new CodeCompileUnit to contain 
    ' the program graph.
    Dim compileUnit As New CodeCompileUnit()

    ' Declare a new namespace called Samples.
    Dim samples As New CodeNamespace("Samples")

    ' Add the new namespace to the compile unit.
    compileUnit.Namespaces.Add(samples)

    ' Add the new namespace import for the System namespace.
    samples.Imports.Add(New CodeNamespaceImport("System"))

    ' Declare a new type called Class1.
    Dim class1 As New CodeTypeDeclaration("Class1")

    ' Add the new type to the namespace type collection.
    samples.Types.Add(class1)

    ' Declare a new code entry point method.
    Dim start As New CodeEntryPointMethod()

    ' Create a type reference for the System.Console class.
    Dim csSystemConsoleType As New CodeTypeReferenceExpression( _
        "System.Console")

    ' Build a Console.WriteLine statement.
    Dim cs1 As New CodeMethodInvokeExpression( _
        csSystemConsoleType, "WriteLine", _
        New CodePrimitiveExpression("Hello World!"))

    ' Add the WriteLine call to the statement collection.
    start.Statements.Add(cs1)

    ' Build another Console.WriteLine statement.
    Dim cs2 As New CodeMethodInvokeExpression( _
        csSystemConsoleType, "WriteLine", _
        New CodePrimitiveExpression("Press the Enter key to continue."))

    ' Add the WriteLine call to the statement collection.
    start.Statements.Add(cs2)

    ' Build a call to System.Console.ReadLine.
    Dim csReadLine As New CodeMethodInvokeExpression( _
        csSystemConsoleType, "ReadLine")

    ' Add the ReadLine statement.
    start.Statements.Add(csReadLine)

    ' Add the code entry point method to
    ' the Members collection of the type.
    class1.Members.Add(start)

    Return compileUnit
End Function

Comentarios

CodeCompileUnitproporciona un contenedor para un gráfico de programa CodeDOM.CodeCompileUnit provides a container for a CodeDOM program graph.

CodeCompileUnitcontiene una colección que puede almacenar CodeNamespace objetos que contienen gráficos de código fuente CodeDom, junto con una colección de ensamblados a los que hace referencia el proyecto y una colección de atributos para el ensamblado del proyecto.CodeCompileUnit contains a collection that can store CodeNamespace objects containing CodeDOM source code graphs, along with a collection of assemblies referenced by the project, and a collection of attributes for the project assembly.

Se puede pasar GenerateCodeFromCompileUnit al método de una ICodeGenerator implementación junto con otros parámetros para generar código basado en el gráfico de programa contenido en la unidad de compilación. CodeCompileUnitA CodeCompileUnit can be passed to the GenerateCodeFromCompileUnit method of an ICodeGenerator implementation along with other parameters to generate code based on the program graph contained by the compile unit.

Nota

Algunos lenguajes admiten un solo espacio de nombres que contiene una sola clase en una unidad de compilación.Some languages support only a single namespace that contains a single class in a compile unit.

Constructores

CodeCompileUnit() CodeCompileUnit() CodeCompileUnit() CodeCompileUnit()

Inicializa una nueva instancia de la clase CodeCompileUnit.Initializes a new instance of the CodeCompileUnit class.

Propiedades

AssemblyCustomAttributes AssemblyCustomAttributes AssemblyCustomAttributes AssemblyCustomAttributes

Obtiene una colección de atributos personalizados para el ensamblado generado.Gets a collection of custom attributes for the generated assembly.

EndDirectives EndDirectives EndDirectives EndDirectives

Obtiene un objeto CodeDirectiveCollection que contiene las directivas End.Gets a CodeDirectiveCollection object containing end directives.

Namespaces Namespaces Namespaces Namespaces

Obtiene la colección de espacios de nombres.Gets the collection of namespaces.

ReferencedAssemblies ReferencedAssemblies ReferencedAssemblies ReferencedAssemblies

Obtiene los ensamblados a los que se hace referencia.Gets the referenced assemblies.

StartDirectives StartDirectives StartDirectives StartDirectives

Obtiene un objeto CodeDirectiveCollection que contiene las directivas Start.Gets a CodeDirectiveCollection object containing start directives.

UserData UserData UserData UserData

Obtiene los datos definibles por el usuario para el objeto actual.Gets the user-definable data for the current object.

(Inherited from CodeObject)

Métodos

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

Determina si el objeto especificado es igual al objeto actual.Determines whether the specified object is equal to the current object.

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

Sirve como la función hash predeterminada.Serves as the default hash function.

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

Obtiene el Type de la instancia actual.Gets the Type of the current instance.

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

Crea una copia superficial del objeto Object actual.Creates a shallow copy of the current Object.

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

Devuelve una cadena que representa el objeto actual.Returns a string that represents the current object.

(Inherited from Object)

Se aplica a