今天无意中看到了.NET Framework中支持的“代码文档对象模型”(CodeDOM) 的机制。该机制使编写源代码的程序的开发人员可以在运行时根据表示所呈现代码的单一模型用多种编程语言生成源代码。感觉功能挺强大也很有应用前景。这里就用一个简单的实例来体会下如何使用CodeDom生成我们再熟悉不过的C#的Helloworld代码。首先创建一个代码编译单元CodeCompileUnit,代码如下:
InBlock.gif private static CodeCompileUnit CreateCompileUnit()
InBlock.gif{
InBlock.gif        CodeCompileUnit compileUnit = new CodeCompileUnit();
InBlock.gif         //Add Namespace
InBlock.gif        CodeNamespace sampleNamespace = new CodeNamespace( "CodeDomSample");
InBlock.gif        compileUnit.Namespaces.Add(sampleNamespace);
InBlock.gif         //Import Namespace: System
InBlock.gif        CodeNamespaceImport systemImport = new CodeNamespaceImport( "System");
InBlock.gif        sampleNamespace.Imports.Add(systemImport);
InBlock.gif         //Add Class
InBlock.gif        CodeTypeDeclaration programType = new CodeTypeDeclaration( "Program");
InBlock.gif        programType.IsClass = true;
InBlock.gif        sampleNamespace.Types.Add(programType);
InBlock.gif         //Add main method
InBlock.gif        CodeEntryPointMethod mainMethod = new CodeEntryPointMethod();
InBlock.gif        programType.Members.Add(mainMethod);
InBlock.gif         //Add statement of main method
InBlock.gif        CodeMethodInvokeExpression hello = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression( "Console"),
InBlock.gif                                                                                                                                             "WriteLine",
InBlock.gif                                                                                                                                             new CodePrimitiveExpression( "Hello World"));
InBlock.gif        CodeExpressionStatement consoleWriteStatement = new CodeExpressionStatement(hello);
InBlock.gif        mainMethod.Statements.Add(consoleWriteStatement);
InBlock.gif         return compileUnit;
InBlock.gif}

其中大致步骤如下:
1. 创建CodeCompileUnit类的实例
2. 在CodeCompileUnit对象中加入命名空间
3. 导入关联的命名空间
4. 在类中加入成员函数
5. 在成员函数中加入处理语句
有了代码编译单元,通过由CodeDomProvider继承的类就可以生成源代码了。

InBlock.gif private void GenerateCodeFile(CodeCompileUnit unit, String fileName)
InBlock.gif{
InBlock.gif        Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
InBlock.gif         string fileName = "HelloWorld." + provider.FileExtension;
InBlock.gif         using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName))
InBlock.gif        {
InBlock.gif                System.CodeDom.Compiler.CodeGeneratorOptions options = new System.CodeDom.Compiler.CodeGeneratorOptions();
InBlock.gif                options.IndentString = "        ";
InBlock.gif                provider.GenerateCodeFromCompileUnit(compileUnit, writer, options);
InBlock.gif                writer.Close();
InBlock.gif        }
InBlock.gif}

生成的源码如下:
InBlock.gif //------------------------------------------------------------------------------
InBlock.gif // <auto-generated>
InBlock.gif //         This code was generated by a tool.
InBlock.gif //         Runtime Version:2.0.50727.4927
InBlock.gif //
InBlock.gif //         Changes to this file may cause incorrect behavior and will be lost if
InBlock.gif //         the code is regenerated.
InBlock.gif // </auto-generated>
InBlock.gif //------------------------------------------------------------------------------
InBlock.gif namespace CodeDomSample {
InBlock.gif         using System;
InBlock.gif         public class Program {
InBlock.gif                 public static void Main() {
InBlock.gif                        Console.WriteLine( "Hello World");
InBlock.gif                }
InBlock.gif        }
InBlock.gif}

  继续了解的过程中,发现很多开源项目也都在使用CodeDom进行代码的自动生成以及不同语言间的代码转换。这方面希望可以在后续文章中继续关注。