CodeCompileUnit Třída

Definice

Poskytuje kontejner pro graf programu CodeDOM.

public ref class CodeCompileUnit : System::CodeDom::CodeObject
public class CodeCompileUnit : System.CodeDom.CodeObject
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CodeCompileUnit : System.CodeDom.CodeObject
type CodeCompileUnit = class
    inherit CodeObject
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
[<System.Serializable>]
type CodeCompileUnit = class
    inherit CodeObject
Public Class CodeCompileUnit
Inherits CodeObject
Dědičnost
CodeCompileUnit
Odvozené
Atributy

Příklady

Následující příklad kódu vytvoří CodeCompileUnit model, který modeluje strukturu programu jednoduchého programu "Hello World". Tento příklad kódu je součástí většího příkladu, který také vytváří kód z tohoto modelu a je k dispozici pro CodeDomProvider třídu.

// 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

Poznámky

CodeCompileUnit poskytuje kontejner pro graf programu CodeDOM.

CodeCompileUnit obsahuje kolekci, která může ukládat CodeNamespace objekty obsahující grafy zdrojového kódu CodeDOM, společně s kolekcí sestavení odkazovaných projektem a kolekci atributů pro sestavení projektu.

Metodu CodeCompileUnitGenerateCodeFromCompileUnitICodeGenerator implementace lze předat společně s dalšími parametry, které vygenerují kód na základě grafu programu obsaženého v kompilační jednotce.

Poznámka

Některé jazyky podporují pouze jeden obor názvů, který obsahuje jednu třídu v jednotce kompilace.

Konstruktory

CodeCompileUnit()

Inicializuje novou instanci CodeCompileUnit třídy.

Vlastnosti

AssemblyCustomAttributes

Získá kolekci vlastních atributů pro vygenerované sestavení.

EndDirectives

CodeDirectiveCollection Získá objekt obsahující koncové direktivy.

Namespaces

Získá kolekci oborů názvů.

ReferencedAssemblies

Získá odkazovaná sestavení.

StartDirectives

CodeDirectiveCollection Získá objekt obsahující direktivy start.

UserData

Získá uživatelsky definovatelná data pro aktuální objekt.

(Zděděno od CodeObject)

Metody

Equals(Object)

Určí, zda se zadaný objekt rovná aktuálnímu objektu.

(Zděděno od Object)
GetHashCode()

Slouží jako výchozí hashovací funkce.

(Zděděno od Object)
GetType()

Získá aktuální Type instanci.

(Zděděno od Object)
MemberwiseClone()

Vytvoří mělkou kopii aktuálního Objectsouboru .

(Zděděno od Object)
ToString()

Vrátí řetězec, který představuje aktuální objekt.

(Zděděno od Object)

Platí pro