CodeCompileUnit Class
Assembly: System (in system.dll)
'Declaration <SerializableAttribute> _ <ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _ <ComVisibleAttribute(True)> _ Public Class CodeCompileUnit Inherits CodeObject 'Usage Dim instance As CodeCompileUnit
/** @attribute SerializableAttribute() */ /** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ /** @attribute ComVisibleAttribute(true) */ public class CodeCompileUnit extends CodeObject
SerializableAttribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) ComVisibleAttribute(true) public class CodeCompileUnit extends CodeObject
Not applicable.
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. 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
System.CodeDom.CodeObject
System.CodeDom.CodeCompileUnit
System.CodeDom.CodeSnippetCompileUnit
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: