使用CodeDom来生成.cs文件

2016-01-29 12:30 9 1 收藏

使用CodeDom来生成.cs文件,使用CodeDom来生成.cs文件

【 tulaoshi.com - ASP.NET 】


在学使用CodeDom来动态生成.cs文件,使用帮助里的例子,代码居然编译不通过自己修改,调试通过,整理后主要代码如下:
命名空间:using System.CodeDom;using System.CodeDom.Compiler;using Microsoft.CSharp;using System.IO;
??private void button1_Click(object sender, System.EventArgs e)??{???CodeCompileUnit CompileUnit = new CodeCompileUnit();???CodeNamespace Samples = new CodeNamespace("Samples");???Samples.Imports.Add( new CodeNamespaceImport("System") );???CompileUnit.Namespaces.Add( Samples );???CodeTypeDeclaration Class1 = new CodeTypeDeclaration("Class1");???Samples.Types.Add(Class1);
???CodeEntryPointMethod Start = new CodeEntryPointMethod();??????//输出HelloWord???CodeMethodInvokeExpression cs1 = new CodeMethodInvokeExpression( new ????CodeTypeReferenceExpression("System.Console"), "WriteLine", new ????CodePrimitiveExpression("Hello World!") );??????Start.Statements.Add(cs1);???

???Class1.Members.Add( Start );???//CSharpCodeProvider provider = new CSharpCodeProvider();???//ICodeGenerator gen = provider.CreateGenerator();???GenerateGraph(CompileUnit);
??}??public void GenerateGraph(CodeCompileUnit compileunit)??{???// Obtains an ICodeGenerator from a CodeDomProvider class.???CSharpCodeProvider provider = new CSharpCodeProvider();???ICodeGenerator gen = provider.CreateGenerator();?? ???// Creates a StreamWriter to an output file.???StreamWriter sw = new StreamWriter("d:TestGraph.cs", false);
???// Generates source code using the code generator.???gen.GenerateCodeFromCompileUnit(compileunit, sw, new??? CodeGeneratorOptions());?? ???// Closes the output files.???sw.Close();??}
??private void button2_Click(object sender, System.EventArgs e)??{???CompileCode("d:TestGraph.cs");??}??//编辑生成Exe??public CompilerResults CompileCode(string filepath)??{???// Obtains an ICodeCompiler from a CodeDomProvider class.???CSharpCodeProvider provider = new CSharpCodeProvider();???ICodeCompiler compiler = provider.CreateCompiler();
???// Configures a compiler parameters object which links System.dll and ???// generates a file name based on the specified source file name.???CompilerParameters cp = new CompilerParameters(new string[] {"System.dll"}, filepath.Substring(0, filepath.LastIndexOf(".")+1)+"exe", false);
???// Indicates that an executable rather than a .dll should be generated.???cp.GenerateExecutable = true;
???// Invokes compilation. ???CompilerResults cr = compiler.CompileAssemblyFromFile(cp, filepath);??
???// Returns the results of compilation.???return cr;??????? ??}
帮助里的例子在:.NET Framework->使用 .NET Framework 编程->动态生成和编译以多种语言表示的源代码

来源:http://www.tulaoshi.com/n/20160129/1486394.html

延伸阅读
标签: PHP
  What is FPDF? FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say without using the PDFlib library. The advantage is that PDFlib requires a fee for a commercial usage. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs. FPDF可以...
标签: Web开发
代码如下: public void exportHibernteToSQL(){ Configuration cfg=new Configuration().configure("/hibernate.cfg.xml"); SchemaExport schemaExport = new SchemaExport(cfg); schemaExport.setOutputFile("d:/mysql_sql.sql"); schemaExport.create(true, false); } 注意:Hibernate的配置文件是什么数据库方言和驱动,将生成什么数...
  近回答了一个问题,是关于根据DFM文件来生成程序的界面的,花了数天的研究,对于一般的程序界面 基本可以还原了。不敢自留,在这里将代码贴出来,里面没有多少解释,可能阅读不大方便,在这里表示 抱歉,本人没有多少时间,所以就请各位有兴趣地自己分析代码了。 其主要思路是用递归的方式来分析DFM文件,再用流化技术将...
标签: Web开发
用php生成excel文件   ? header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:filename=test.xls"); echo "test1\t"; echo "test2\t\n"; echo "test1\t"; echo "test2\t\n"; echo "test1\t"; echo "test2\t\n"; echo &...
标签: ASP
  data1(3,6,0)=0 data1(3,6,1)=0 data1(3,6,2)=0 data1(3,6,3)=0 data1(3,6,4)=0 data1(3,6,5)=0 data1(3,6,6)=0 data1(3,6,7)=1 data1(3,5,0)=0 data1(3,5,1)=0 data1(3,5,2)=0 data1(3,5,3)=0 data1(3,5,4)=0 data1(3,5,5)=0 data1(3,5,6)=0 data1(3,5,7)=1 data1(3,4,0)=0 data1(3,4,1)=1 data1(3,4,2)=1 data1(3,4,3)=1 data1(3,4,...

经验教程

406

收藏

61
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部