Export (0) Print
Expand All
1 out of 4 rated this helpful - Rate this topic

CodeCompileUnit Class

Provides a container for a CodeDOM program graph.

For a list of all members of this type, see CodeCompileUnit Members.

System.Object
   System.CodeDom.CodeObject
      System.CodeDom.CodeCompileUnit
         System.CodeDom.CodeSnippetCompileUnit

[Visual Basic]
<Serializable>
<ClassInterface(ClassInterfaceType.AutoDispatch)>
<ComVisible(True)>
Public Class CodeCompileUnit
   Inherits CodeObject
[C#]
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ComVisible(true)]
public class CodeCompileUnit : CodeObject
[C++]
[Serializable]
[ClassInterface(ClassInterfaceType::AutoDispatch)]
[ComVisible(true)]
public __gc class CodeCompileUnit : public CodeObject
[JScript]
public
   Serializable
 ClassInterface(ClassInterfaceType.AutoDispatch)
 ComVisible(true)
class CodeCompileUnit extends CodeObject

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

CodeCompileUnit provides a container for a CodeDOM program graph.

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.

A CodeCompileUnit can be passed to the GenerateCodeFromCompileUnit method of an ICodeGenerator implementation along with other parameters to generate code based upon the program graph contained by the compile unit.

Note   Some languages support only a single namespace that contains a single class in a compile unit.

Example

[Visual Basic, C#, C++] The following example constructs a CodeCompileUnit that models the program structure of a simple "Hello World" program. For a larger code example that produces code from this model, see the code example for the CodeDomProvider class.

[Visual Basic] 
' 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 

[C#] 
// 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;
}        

[C++] 
// Build a Hello World program graph using 
// System::CodeDom types.
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(S"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(S"System"));

    // Declare a new type called Class1.
    CodeTypeDeclaration* class1 = new CodeTypeDeclaration(S"Class1");
    // Add the new type to the namespace's type collection.
    samples->Types->Add(class1);

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

    CodePrimitiveExpression *codePrimitive[] = new CodePrimitiveExpression*[1];

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

    // Build a Console::WriteLine statement.
    codePrimitive[0] = new CodePrimitiveExpression(S"Hello World!");
    CodeMethodInvokeExpression* cs1 = new CodeMethodInvokeExpression(
        csSystemConsoleType, 
        S"WriteLine",
        static_cast<CodeExpression __gc * __gc[]>(codePrimitive)
        );

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

    // Build another Console::WriteLine statement.
    codePrimitive[0] = new CodePrimitiveExpression(S"Press the Enter key to continue.");
    CodeMethodInvokeExpression* cs2 = new CodeMethodInvokeExpression(
        csSystemConsoleType, 
        S"WriteLine",
        static_cast<CodeExpression __gc * __gc[]>(codePrimitive)
        );
    // Add the WriteLine call to the statement collection.
    start->Statements->Add(cs2);

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

    CodeExpression *blankParms[] = new CodeExpression*[0];
    CodeMethodInvokeExpression *cs3 = new CodeMethodInvokeExpression(
        csReadLine, static_cast<CodeExpression __gc * __gc[]>(blankParms));

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

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.CodeDom

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

CodeCompileUnit Members | System.CodeDom Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.