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