CodeCompileUnit CodeCompileUnit CodeCompileUnit CodeCompileUnit Class

定義

提供一個 CodeDOM 程式圖形的容器。Provides a container for a CodeDOM program graph.

public ref class CodeCompileUnit : System::CodeDom::CodeObject
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
[System.Serializable]
public class CodeCompileUnit : System.CodeDom.CodeObject
type CodeCompileUnit = class
    inherit CodeObject
Public Class CodeCompileUnit
Inherits CodeObject
繼承
CodeCompileUnitCodeCompileUnitCodeCompileUnitCodeCompileUnit
衍生
屬性

範例

下列程式碼範例會建立CodeCompileUnit模型, 它會將簡單「Hello World」程式的程式結構模型。The following code example constructs a CodeCompileUnit that models the program structure of a simple "Hello World" program. 這個程式碼範例是較大範例的一部分, 它也會從這個模型產生程式碼, 並CodeDomProvider提供給類別。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;
}
// 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;
}
' 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

備註

CodeCompileUnit提供 CodeDOM 程式圖形的容器。CodeCompileUnit provides a container for a CodeDOM program graph.

CodeCompileUnit包含的集合可以儲存CodeNamespace包含 CodeDOM 原始程式碼圖形的物件, 以及專案所參考的元件集合和專案元件的屬性集合。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.

可以傳遞ICodeGenerator至執行方法和其他參數, 以根據編譯單位所包含的程式圖形來產生程式碼。 GenerateCodeFromCompileUnit CodeCompileUnitA 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.

建構函式

CodeCompileUnit() CodeCompileUnit() CodeCompileUnit() CodeCompileUnit()

初始化 CodeCompileUnit 類別的新執行個體。Initializes a new instance of the CodeCompileUnit class.

屬性

AssemblyCustomAttributes AssemblyCustomAttributes AssemblyCustomAttributes AssemblyCustomAttributes

取得已產生組件之自訂屬性的集合。Gets a collection of custom attributes for the generated assembly.

EndDirectives EndDirectives EndDirectives EndDirectives

取得包含結尾指示詞的 CodeDirectiveCollection 物件。Gets a CodeDirectiveCollection object containing end directives.

Namespaces Namespaces Namespaces Namespaces

取得命名空間的集合。Gets the collection of namespaces.

ReferencedAssemblies ReferencedAssemblies ReferencedAssemblies ReferencedAssemblies

取得參考的組件。Gets the referenced assemblies.

StartDirectives StartDirectives StartDirectives StartDirectives

取得包含開頭指示詞的 CodeDirectiveCollection 物件。Gets a CodeDirectiveCollection object containing start directives.

UserData UserData UserData UserData

取得目前物件的使用者可定義資料。Gets the user-definable data for the current object.

(Inherited from CodeObject)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於