.NET Framework Class Library  

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 safe for multithreaded operations. 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] 
' Builds 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 a 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's type collection.
    Samples.Types.Add(Class1)

    ' Declare a new code entry point method.
    Dim Start As New CodeEntryPointMethod()
    ' Create a new method invocation expression.
    Dim cs1 As New CodeMethodInvokeExpression(New CodeTypeReferenceExpression("System.Console"), "WriteLine", New CodePrimitiveExpression("Hello World!"))
    ' Call the System.Console.WriteLine method.
    ' Pass a primitive string parameter to the WriteLine method.
    ' Add the new method code statement.
    Start.Statements.Add(New CodeExpressionStatement(cs1))

    ' Add the code entry point method to the type's members collection.
    Class1.Members.Add(Start)

    Return CompileUnit
End Function 

[C#] 
// Builds 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's type collection.
    Samples.Types.Add(Class1);
    
    // Declare a new code entry point method
    CodeEntryPointMethod Start = new CodeEntryPointMethod();
    // Create a new method invocation expression.
    CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression( 
        // Call the System.Console.WriteLine method.
        new CodeTypeReferenceExpression("System.Console"), "WriteLine", 
        // Pass a primitive string parameter to the WriteLine method
        new CodePrimitiveExpression("Hello World!") );
    // Add the new method code statement.
    Start.Statements.Add(new CodeExpressionStatement(cs1));

    // Add the code entry point method to the type's members collection
    Class1.Members.Add( Start );

    return CompileUnit;
}

[C++] 
// Builds a Hello World program graph using namespace 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(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();
   // Create a new method invocation expression.
   CodePrimitiveExpression *cpe[] = new CodePrimitiveExpression*[1];
   cpe[0] = new CodePrimitiveExpression(S"Hello World!");
   CodeMethodInvokeExpression* cs1 = new CodeMethodInvokeExpression(
      // Call the System::Console::WriteLine method.
      new CodeTypeReferenceExpression(S"System.Console"), 
      S"WriteLine",
      static_cast<CodeExpression __gc * __gc[]>
      (// Pass a primitive String* parameter to the WriteLine method
      cpe
      )
      );
   // Add the new method code statement.
   Start->Statements->Add(new CodeExpressionStatement(cs1));

   // Add the code entry point method to the type's members collection
   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