使用 CodeDOMUsing the CodeDOM

CodeDOM 提供的類型代表許多常見的原始程式碼項目類型。The CodeDOM provides types that represent many common types of source code elements. 您可以設計程式,建置使用 CodeDOM 項目的原始程式碼模型,組合物件圖形。You can design a program that builds a source code model using CodeDOM elements to assemble an object graph. 此物件圖形可以轉譯成使用 CodeDOM 程式碼產生器的原始程式碼,處理受支援的程式設計語言。This object graph can be rendered as source code using a CodeDOM code generator for a supported programming language. 您也可以使用 CodeDOM 將原始程式碼編譯成二進位組件。The CodeDOM can also be used to compile source code into a binary assembly.

常見的 CodeDOM 用法包括:Some common uses for the CodeDOM include:

  • 產生樣板化程式碼:為 ASP.NET、XML Web 服務用戶端 proxy、程式碼精靈、設計工具,或其他程式碼發出機制產生程式碼。Templated code generation: generating code for ASP.NET, XML Web services client proxies, code wizards, designers, or other code-emitting mechanisms.

  • 動態編譯:支援以單一或多種語言編譯程式碼。Dynamic compilation: supporting code compilation in single or multiple languages.

建置 CodeDOM 圖形Building a CodeDOM Graph

System.CodeDom 命名空間提供代表原始程式碼邏輯結構的類別,不受語言語法影響。The System.CodeDom namespace provides classes for representing the logical structure of source code, independent of language syntax.

CodeDOM 圖形的結構The Structure of a CodeDOM Graph

CodeDOM 圖形的結構就像容器的樹狀結構。The structure of a CodeDOM graph is like a tree of containers. 每個可編譯 CodeDOM 圖形的最上層 (或根) 容器都是 CodeCompileUnitThe top-most, or root, container of each compilable CodeDOM graph is a CodeCompileUnit. 原始程式碼模型的每個項目都必須透過圖形中 CodeObject 屬性連結到圖形。Every element of your source code model must be linked into the graph through a property of a CodeObject in the graph.

建置範例 Hello World 程式的原始程式碼模型Building a Source Code Model for a Sample Hello World Program

下列逐步解說提供如何建置 CodeDOM 物件圖形的範例,此物件圖形代表簡單的 Hello World 應用程式程式碼。The following walkthrough provides an example of how to build a CodeDOM object graph that represents the code for a simple Hello World application. 如需此程式碼範例的完整原始程式碼,請參閱 System.CodeDom.Compiler.CodeDomProvider 主題。For the complete source code for this code example, see the System.CodeDom.Compiler.CodeDomProvider topic.

建立編譯單位Creating a compile unit

CodeDOM 會定義名為 CodeCompileUnit 的物件,如此可參考 CodeDOM 物件圖形,建立要編譯的原始程式碼模型。The CodeDOM defines an object called a CodeCompileUnit, which can reference a CodeDOM object graph that models the source code to compile. CodeCompileUnit 的屬性可以儲存屬性、命名空間和組件的參考。A CodeCompileUnit has properties for storing references to attributes, namespaces, and assemblies.

衍生自 CodeDomProvider 類別的 CodeDom 提供者有方法可處理 CodeCompileUnit 所參考的物件圖形。The CodeDom providers that derive from the CodeDomProvider class contain methods that process the object graph referenced by a CodeCompileUnit.

若要建立簡單應用程式的物件圖形,您必須組合原始程式碼模型,並從 CodeCompileUnit 參考它。To create an object graph for a simple application, you must assemble the source code model and reference it from a CodeCompileUnit.

您可以使用本例示範的語法,建立新的編譯單位:You can create a new compile unit with the syntax demonstrated in this example:

CodeCompileUnit^ compileUnit = gcnew CodeCompileUnit();
CodeCompileUnit compileUnit = new CodeCompileUnit();
Dim compileUnit As New CodeCompileUnit()

CodeSnippetCompileUnit 可以包含已使用目標語言的原始程式碼區段,但無法轉譯成其他語言。A CodeSnippetCompileUnit can contain a section of source code that is already in the target language, but cannot be rendered to another language.

定義命名空間Defining a namespace

若要定義命名空間,請使用適當的建構函式,或設定其 Name 屬性,建立 CodeNamespace 並指派其名稱。To define a namespace, create a CodeNamespace and assign a name for it using the appropriate constructor or by setting its Name property.

CodeNamespace^ samples = gcnew CodeNamespace("Samples");
CodeNamespace samples = new CodeNamespace("Samples");
Dim samples As New CodeNamespace("Samples")

匯入命名空間Importing a namespace

若要將命名空間匯入指示詞新增至命名空間,請新增 CodeNamespaceImport,指示命名空間要匯入 CodeNamespace.Imports 集合。To add a namespace import directive to the namespace, add a CodeNamespaceImport that indicates the namespace to import to the CodeNamespace.Imports collection.

下列程式碼會將匯入的 System 命名空間新增至名為 samplesCodeNamespaceImports 集合:The following code adds an import for the System namespace to the Imports collection of a CodeNamespace named samples:

samples->Imports->Add(gcnew CodeNamespaceImport("System"));
samples.Imports.Add(new CodeNamespaceImport("System"));
samples.Imports.Add(new CodeNamespaceImport("System"))

將程式碼項目連結到物件圖形Linking code elements into the object graph

形成 CodeDOM 圖形的所有程式碼項目都必須連結到 CodeCompileUnit,亦即樹狀結構的根項目,此樹狀結構是圖形根物件屬性直接參考項目間的一系列參考。All code elements that form a CodeDOM graph must be linked to the CodeCompileUnit that is the root element of the tree by a series of references between elements directly referenced from the properties of the root object of the graph. 設定容器物件屬性的物件,從容器物件建立參考。Set an object to a property of a container object to establish a reference from the container object.

下列陳述式會將 samples CodeNamespace 新增至根 CodeCompileUnitNamespaces 集合屬性。The following statement adds the samples CodeNamespace to the Namespaces collection property of the root CodeCompileUnit.

compileUnit->Namespaces->Add( samples );
compileUnit.Namespaces.Add( samples );
compileUnit.Namespaces.Add(samples)

定義類型Defining a type

若要宣告使用 CodeDOM 的類別、結構、介面或列舉,請建立新的 CodeTypeDeclaration,並為它指派名稱。To declare a class, structure, interface, or enumeration using the CodeDOM, create a new CodeTypeDeclaration, and assign it a name. 下列範例會使用建構函式多載設定 Name 屬性,來示範此作業:The following example demonstrates this using a constructor overload to set the Name property:

CodeTypeDeclaration^ class1 = gcnew CodeTypeDeclaration("Class1");
CodeTypeDeclaration class1 = new CodeTypeDeclaration("Class1");
Dim class1 As New CodeTypeDeclaration("Class1")

若要將類型新增到命名空間,請新增代表類型的 CodeTypeDeclaration,新增至 CodeNamespaceTypes 集合的命名空間。To add a type to a namespace, add a CodeTypeDeclaration that represents the type to add to the namespace to the Types collection of a CodeNamespace.

下列範例示範如何將名為 class1 的類別新增至名為 samplesCodeNamespaceThe following example demonstrates how to add a class named class1 to a CodeNamespace named samples:

samples->Types->Add(class1);
samples.Types.Add(class1);
samples.Types.Add(class1)

將類別成員新增至類別Adding class members to a class

System.CodeDom 命名空間提供各種不同的項目,可以用來代表類別成員。The System.CodeDom namespace provides a variety of elements that can be used to represent class members. 每個類別成員皆可新增至 CodeTypeDeclarationMembers 集合。Each class member can be added to the Members collection of a CodeTypeDeclaration.

定義可執行檔的程式碼進入點方法Defining a code entry point method for an executable

如果您要為可執行程式建置程式碼,就必須建立 CodeEntryPointMethod 代表程式應該開始執行的那個方法,指示程式的進入點。If you are building code for an executable program, it is necessary to indicate the entry point of a program by creating a CodeEntryPointMethod to represent the method at which program execution should begin.

下例示範如何定義包含 CodeMethodInvokeExpression 的進入點方法,呼叫 System.Console.WriteLine 列印 "Hello World!":The following example demonstrates how to define an entry point method that contains a CodeMethodInvokeExpression that calls System.Console.WriteLine to print "Hello World!":

CodeEntryPointMethod^ start = gcnew CodeEntryPointMethod();
CodeMethodInvokeExpression^ cs1 = gcnew CodeMethodInvokeExpression(
    gcnew CodeTypeReferenceExpression("System.Console"),
    "WriteLine", gcnew CodePrimitiveExpression("Hello World!"));
start->Statements->Add(cs1);
CodeEntryPointMethod start = new CodeEntryPointMethod();
CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression(
    new CodeTypeReferenceExpression("System.Console"),
    "WriteLine", new CodePrimitiveExpression("Hello World!"));
start.Statements.Add(cs1);
Dim start As New CodeEntryPointMethod()
Dim cs1 As New CodeMethodInvokeExpression( _
    New CodeTypeReferenceExpression("System.Console"), _
    "WriteLine", new CodePrimitiveExpression("Hello World!"))
start.Statements.Add(cs1)

下列陳述式會將名為 Start 的進入點方法新增至 class1Members 集合:The following statement adds the entry point method named Start to the Members collection of class1:

class1->Members->Add(start);
class1.Members.Add( start );
class1.Members.Add( start)

現在名為 compileUnitCodeCompileUnit 包含簡單 Hello World 程式的 CodeDOM 圖形。Now the CodeCompileUnit named compileUnit contains the CodeDOM graph for a simple Hello World program. 如需從 CodeDOM 圖形產生及編譯程式碼的相關資訊,請參閱從 CodeDOM 圖形產生原始程式碼和編譯程式For information on generating and compiling code from a CodeDOM graph, see Generating Source Code and Compiling a Program from a CodeDOM Graph.

建置 CodeDOM 圖形的詳細資訊More information on building a CodeDOM graph

CodeDOM 支援許多程式設計語言中常見的程式碼項目類型,這些程式設計語言都支援 Common Language Runtime。The CodeDOM supports the many common types of code elements found in programming languages that support the common language runtime. CodeDOM 的設計目的不是提供代表所有可能程式設計語言功能的項目。The CodeDOM was not designed to provide elements to represent all possible programming language features. 無法簡單以 CodeDOM 項目表示的程式碼,可以封裝在 CodeSnippetExpressionCodeSnippetStatementCodeSnippetTypeMemberCodeSnippetCompileUnit 中。Code that cannot be represented easily with CodeDOM elements can be encapsulated in a CodeSnippetExpression, a CodeSnippetStatement, a CodeSnippetTypeMember, or a CodeSnippetCompileUnit. 不過,CodeDOM 無法自動將程式碼片段轉譯成其他語言。However, snippets cannot be translated to other languages automatically by the CodeDOM.

如需 CodeDOM 各類型的文件,請參閱 System.CodeDom 命名空間的參考文件。For documentation for the each of the CodeDOM types, see the reference documentation for the System.CodeDom namespace.

如需快速圖表,找出表示特定程式碼項目類型的 CodeDOM 項目,請參閱 CodeDOM 快速參考For a quick chart to locate the CodeDOM element that represents a specific type of code element, see the CodeDOM Quick Reference.