CodeCompileUnit Class
Provides a container for a CodeDOM program graph.
Assembly: System (in System.dll)
System.CodeDom::CodeObject
System.CodeDom::CodeCompileUnit
System.CodeDom::CodeSnippetCompileUnit
| Name | Description | |
|---|---|---|
![]() | CodeCompileUnit() | Initializes a new instance of the CodeCompileUnit class. |
| Name | Description | |
|---|---|---|
![]() | AssemblyCustomAttributes | Gets a collection of custom attributes for the generated assembly. |
![]() | EndDirectives | Gets a CodeDirectiveCollection object containing end directives. |
![]() | Namespaces | Gets the collection of namespaces. |
![]() | ReferencedAssemblies | Gets the referenced assemblies. |
![]() | StartDirectives | Gets a CodeDirectiveCollection object containing start directives. |
![]() | UserData | Gets the user-definable data for the current object.(Inherited from CodeObject.) |
| Name | Description | |
|---|---|---|
![]() | Equals(Object^) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | Finalize() | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.) |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetType() | |
![]() | MemberwiseClone() | |
![]() | ToString() | Returns 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.
Note |
|---|
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. 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; }
Available since 1.1
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.



