This documentation is archived and is not being maintained.

CodeCompileUnit Class

Provides a container for a CodeDOM program graph.

Namespace:  System.CodeDom
Assembly:  System (in System.dll)

public class CodeCompileUnit : CodeObject

The CodeCompileUnit type exposes the following members.

Public methodCodeCompileUnitInitializes a new instance of the CodeCompileUnit class.

Public propertyAssemblyCustomAttributesGets a collection of custom attributes for the generated assembly.
Public propertyEndDirectivesGets a CodeDirectiveCollection object containing end directives.
Public propertyNamespacesGets the collection of namespaces.
Public propertyReferencedAssembliesGets the referenced assemblies.
Public propertyStartDirectivesGets a CodeDirectiveCollection object containing start directives.
Public propertyUserDataGets the user-definable data for the current object. (Inherited from CodeObject.)

Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

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 on the program graph contained by the compile unit.


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

The following code example constructs a CodeCompileUnit that models the program structure of a simple "Hello World" program. 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.
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.

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

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

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

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

    // Add the ReadLine statement.

    // Add the code entry point method to
    // the Members collection of the type.

    return compileUnit;

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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