閱讀英文版本

分享方式:


CodeCompileUnit 類別

定義

提供一個 CodeDOM 程式圖形的容器。

C#
public class CodeCompileUnit : System.CodeDom.CodeObject
C#
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class CodeCompileUnit : System.CodeDom.CodeObject
繼承
CodeCompileUnit
衍生
屬性

範例

下列程式代碼範例會CodeCompileUnit建構模型化簡單 「Hello World」 程序的程序結構。 此程式代碼範例是較大型範例的一部分,也會從此模型產生程序代碼,並提供給 CodeDomProvider 類別。

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

備註

CodeCompileUnit 提供 CodeDOM 程式圖形的容器。

CodeCompileUnit 包含集合,可儲存 CodeNamespace 包含 CodeDOM 原始程式碼圖形的物件,以及專案元件參考的元件集合,以及專案元件的屬性集合。

CodeCompileUnit可以傳遞至GenerateCodeFromCompileUnit實作ICodeGenerator的 方法,以及其他參數,根據編譯單位所包含的程式圖形產生程序代碼。

備註

某些語言僅支援單一命名空間,其中包含編譯單位中的單一類別。

建構函式

CodeCompileUnit()

初始化 CodeCompileUnit 類別的新執行個體。

屬性

AssemblyCustomAttributes

取得已產生組件之自訂屬性的集合。

EndDirectives

取得包含結尾指示詞的 CodeDirectiveCollection 物件。

Namespaces

取得命名空間的集合。

ReferencedAssemblies

取得參考的組件。

StartDirectives

取得包含開頭指示詞的 CodeDirectiveCollection 物件。

UserData

取得目前物件的使用者可定義資料。

(繼承來源 CodeObject)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

適用於

產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9