代码文档模型
//1、生成c#CodeDom
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.IO;
using System.Collections;
namespace DemoCodeDOM
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
GenerateCode();
System.Console.WriteLine("Generate Code OK");
CompilerResults crt = CompileCode("GenCodeHello.cs");
//下面是编译的结果
// If errors occurred during compilation, output the compiler output and errors.
if( crt.Errors.Count > 0 ) {
for( int i=0; i<crt.Output.Count; i++ )
Console.WriteLine( crt.Output[i] );
for( int i=0; i<crt.Errors.Count; i++ )
Console.WriteLine( i.ToString() + ": " + crt.Errors[i].ToString() );
}
else {
// Display information about the compiler's exit code and the generated assembly.
Console.WriteLine( "Compiler returned with result code: " + crt.NativeCompilerReturnValue.ToString() );
Console.WriteLine( "Generated assembly name: " + crt.CompiledAssembly.FullName );
if( crt.PathToAssembly == null )
Console.WriteLine( "The assembly has been generated in memory." );
else
Console.WriteLine( "Path to assembly: " + crt.PathToAssembly );
// Display temporary files information.
if( !crt.TempFiles.KeepFiles )
Console.WriteLine( "Temporary build files were deleted." );
else {
Console.WriteLine( "Temporary build files were not deleted." );
// Display a list of the temporary build files
IEnumerator enu = crt.TempFiles.GetEnumerator();
for( int i=0; enu.MoveNext(); i++ )
Console.WriteLine( "TempFile " + i.ToString() + ": " + (string)enu.Current );
}
}
System.Console.Read();
}
//生成CodeDOM图,这一步是最复杂的部分,后面生成代码与编译都是以这里的东西为蓝本
private static CodeCompileUnit ComplieUnit() {
//生成一个可编译的单元,这是最根部的东西
CodeCompileUnit compunit = new CodeCompileUnit();
CodeNamespace sample = new CodeNamespace("Sample");//定义一个名为Sample的命名空间
CodeTypeDeclaration MyClass = new CodeTypeDeclaration("DemoClass");//定义一个名为DemoClass的类
CodeEntryPointMethod Start = new CodeEntryPointMethod();//定义程序入口点,就是Main()
//下面两句产生调用方法的语句
CodeMethodInvokeExpression cs = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"),
"WriteLine",new CodePrimitiveExpression("Hello World!"));//这句会产生如下的C#代码 System.Console.WriteLine("Hello World!");
CodeMethodInvokeExpression wt = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"),
"Read");//这句是 System.Console.Read();
//下面一系列语句把上述定义好的元素联接起来
compunit.Namespaces.Add(sample);
sample.Imports.Add(new CodeNamespaceImport("System"));//导入System命名空间
sample.Types.Add(MyClass);
MyClass.Members.Add(Start);
Start.Statements.Add(cs);
Start.Statements.Add(wt);
return compunit;
}
//根据CodeDOM产生程序代码,代码文件就是GenCodeHello.cs
public static void GenerateCode() {
CSharpCodeProvider cprovider = new CSharpCodeProvider();//CSharpCodeProvider//当然换个CSharpCodeProvider就产生VB.NET的代码
ICodeGenerator gen = cprovider.CreateGenerator();
StreamWriter sw = new StreamWriter("GenCodeHello.cs",false);
gen.GenerateCodeFromCompileUnit(ComplieUnit(),sw,new CodeGeneratorOptions());
sw.Close();
}
//编译源代码
public static CompilerResults CompileCode(string filepath) {
CSharpCodeProvider cprovider = new CSharpCodeProvider();
ICodeCompiler compiler = cprovider.CreateCompiler();
//编译参数
CompilerParameters cp = new CompilerParameters(new string[] {"System.dll"},filepath.Substring(0,filepath.LastIndexOf(".") + 1) + "exe",false);
cp.GenerateExecutable = true;//生成EXE,不是DLL
CompilerResults cr = compiler.CompileAssemblyFromFile(cp,filepath);
return cr;
}
}
}
//2、生成VBcodeDOM
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.IO;
using System.Collections;
namespace DemoCodeDOM
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
GenerateCode();
System.Console.WriteLine("Generate Code OK");
CompilerResults crt = CompileCode("GenCodeHello.cs");
//下面是编译的结果
// If errors occurred during compilation, output the compiler output and errors.
if( crt.Errors.Count > 0 ) {
for( int i=0; i<crt.Output.Count; i++ )
Console.WriteLine( crt.Output[i] );
for( int i=0; i<crt.Errors.Count; i++ )
Console.WriteLine( i.ToString() + ": " + crt.Errors[i].ToString() );
}
else {
// Display information about the compiler's exit code and the generated assembly.
Console.WriteLine( "Compiler returned with result code: " + crt.NativeCompilerReturnValue.ToString() );
Console.WriteLine( "Generated assembly name: " + crt.CompiledAssembly.FullName );
if( crt.PathToAssembly == null )
Console.WriteLine( "The assembly has been generated in memory." );
else
Console.WriteLine( "Path to assembly: " + crt.PathToAssembly );
// Display temporary files information.
if( !crt.TempFiles.KeepFiles )
Console.WriteLine( "Temporary build files were deleted." );
else {
Console.WriteLine( "Temporary build files were not deleted." );
// Display a list of the temporary build files
IEnumerator enu = crt.TempFiles.GetEnumerator();
for( int i=0; enu.MoveNext(); i++ )
Console.WriteLine( "TempFile " + i.ToString() + ": " + (string)enu.Current );
}
}
System.Console.Read();
}
//生成CodeDOM图,这一步是最复杂的部分,后面生成代码与编译都是以这里的东西为蓝本
private static CodeCompileUnit ComplieUnit() {
//生成一个可编译的单元,这是最根部的东西
CodeCompileUnit compunit = new CodeCompileUnit();
CodeNamespace sample = new CodeNamespace("Sample");//定义一个名为Sample的命名空间
CodeTypeDeclaration MyClass = new CodeTypeDeclaration("DemoClass");//定义一个名为DemoClass的类
CodeEntryPointMethod Start = new CodeEntryPointMethod();//定义程序入口点,就是Main()
//下面两句产生调用方法的语句
CodeMethodInvokeExpression cs = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"),
"WriteLine",new CodePrimitiveExpression("Hello World!"));//这句会产生如下的C#代码 System.Console.WriteLine("Hello World!");
CodeMethodInvokeExpression wt = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Console"),
"Read");//这句是 System.Console.Read();
//下面一系列语句把上述定义好的元素联接起来
compunit.Namespaces.Add(sample);
sample.Imports.Add(new CodeNamespaceImport("System"));//导入System命名空间
sample.Types.Add(MyClass);
MyClass.Members.Add(Start);
Start.Statements.Add(cs);
Start.Statements.Add(wt);
return compunit;
}
//根据CodeDOM产生程序代码,代码文件就是GenCodeHello.cs
public static void GenerateCode() {
Microsoft.VisualBasic.VBCodeProvider cprovider = new Microsoft.VisualBasic.VBCodeProvider();//CSharpCodeProvider//当然换个Microsoft.VisualBasic.VBCodeProvider就产生VB.NET的代码
ICodeGenerator gen = cprovider.CreateGenerator();
StreamWriter sw = new StreamWriter("GenCodeHello.cs",false);
gen.GenerateCodeFromCompileUnit(ComplieUnit(),sw,new CodeGeneratorOptions());
sw.Close();
}
//编译源代码
public static CompilerResults CompileCode(string filepath) {
Microsoft.VisualBasic.VBCodeProvider cprovider = new Microsoft.VisualBasic.VBCodeProvider();
ICodeCompiler compiler = cprovider.CreateCompiler();
//编译参数
CompilerParameters cp = new CompilerParameters(new string[] {"System.dll"},filepath.Substring(0,filepath.LastIndexOf(".") + 1) + "exe",false);
cp.GenerateExecutable = true;//生成EXE,不是DLL
CompilerResults cr = compiler.CompileAssemblyFromFile(cp,filepath);
return cr;
}
}
}