今天无意中看到了.NET Framework中支持的“代码文档对象模型”(CodeDOM) 的机制。该机制使编写源代码的程序的开发人员可以在运行时根据表示所呈现代码的单一模型用多种编程语言生成源代码。感觉功能挺强大也很有应用前景。这里就用一个简单的实例来体会下如何使用CodeDom生成我们再熟悉不过的C#的Helloworld代码。首先创建一个代码编译单元CodeCompileUnit,代码如下:
private
static CodeCompileUnit CreateCompileUnit()
{
CodeCompileUnit compileUnit = new CodeCompileUnit();
//Add Namespace
CodeNamespace sampleNamespace = new CodeNamespace( "CodeDomSample");
compileUnit.Namespaces.Add(sampleNamespace);
//Import Namespace: System
CodeNamespaceImport systemImport = new CodeNamespaceImport( "System");
sampleNamespace.Imports.Add(systemImport);
//Add Class
CodeTypeDeclaration programType = new CodeTypeDeclaration( "Program");
programType.IsClass = true;
sampleNamespace.Types.Add(programType);
//Add main method
CodeEntryPointMethod mainMethod = new CodeEntryPointMethod();
programType.Members.Add(mainMethod);
//Add statement of main method
CodeMethodInvokeExpression hello = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression( "Console"),
"WriteLine",
new CodePrimitiveExpression( "Hello World"));
CodeExpressionStatement consoleWriteStatement = new CodeExpressionStatement(hello);
mainMethod.Statements.Add(consoleWriteStatement);
return compileUnit;
}
{
CodeCompileUnit compileUnit = new CodeCompileUnit();
//Add Namespace
CodeNamespace sampleNamespace = new CodeNamespace( "CodeDomSample");
compileUnit.Namespaces.Add(sampleNamespace);
//Import Namespace: System
CodeNamespaceImport systemImport = new CodeNamespaceImport( "System");
sampleNamespace.Imports.Add(systemImport);
//Add Class
CodeTypeDeclaration programType = new CodeTypeDeclaration( "Program");
programType.IsClass = true;
sampleNamespace.Types.Add(programType);
//Add main method
CodeEntryPointMethod mainMethod = new CodeEntryPointMethod();
programType.Members.Add(mainMethod);
//Add statement of main method
CodeMethodInvokeExpression hello = new CodeMethodInvokeExpression( new CodeTypeReferenceExpression( "Console"),
"WriteLine",
new CodePrimitiveExpression( "Hello World"));
CodeExpressionStatement consoleWriteStatement = new CodeExpressionStatement(hello);
mainMethod.Statements.Add(consoleWriteStatement);
return compileUnit;
}
其中大致步骤如下:
1. 创建CodeCompileUnit类的实例
2. 在CodeCompileUnit对象中加入命名空间
3. 导入关联的命名空间
4. 在类中加入成员函数
5. 在成员函数中加入处理语句
有了代码编译单元,通过由CodeDomProvider继承的类就可以生成源代码了。
private
void GenerateCodeFile(CodeCompileUnit unit, String fileName)
{
Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
string fileName = "HelloWorld." + provider.FileExtension;
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName))
{
System.CodeDom.Compiler.CodeGeneratorOptions options = new System.CodeDom.Compiler.CodeGeneratorOptions();
options.IndentString = " ";
provider.GenerateCodeFromCompileUnit(compileUnit, writer, options);
writer.Close();
}
}
{
Microsoft.CSharp.CSharpCodeProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
string fileName = "HelloWorld." + provider.FileExtension;
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fileName))
{
System.CodeDom.Compiler.CodeGeneratorOptions options = new System.CodeDom.Compiler.CodeGeneratorOptions();
options.IndentString = " ";
provider.GenerateCodeFromCompileUnit(compileUnit, writer, options);
writer.Close();
}
}
生成的源码如下:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.4927
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace CodeDomSample {
using System;
public class Program {
public static void Main() {
Console.WriteLine( "Hello World");
}
}
}
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.4927
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace CodeDomSample {
using System;
public class Program {
public static void Main() {
Console.WriteLine( "Hello World");
}
}
}
继续了解的过程中,发现很多开源项目也都在使用CodeDom进行代码的自动生成以及不同语言间的代码转换。这方面希望可以在后续文章中继续关注。
转载于:https://blog.51cto.com/bj007/345675