当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 使用CodeDom来生成.cs文件

ASP.NET
.Net中使用com组件后发生System.ArithmeticException异常的解决办法
SQL Server.net 和 OLE DB.net连接数据库的比较
后台更新DataTable行内容的方法
敏捷软件开发(原则,模式与实践)笔记1
确保文本框输入值为数值的代码
XML和数据库之间相互的映射
让你的.NET程序兼容不同版本的Dll文件。
.NET 的数据访问应用程序块(Data Access Application Block)
用控件仅一条指令实现界面换肤和多语言版本(YFSkins)
Microsoft User Interface Process Application Block 研究(3)
分享:处理Excel方法小结
基于ASP.NET实现全球化
.net 里面 protected private 的变量也可以访问(新发现)。
关于C#中{0}和{1}的问题初次在此发贴,问题对你易对我难,求救了
使用C#代码实现增加用户帐号
全世界都在关注-微软重大产品发布
教你做Rational Rose(UML Design)
OLE DB取得数据库的架构信息
VB 从零开始编外挂(三)
XPath序列之四

ASP.NET 中的 使用CodeDom来生成.cs文件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 78 ::
收藏到网摘: n/a


在学使用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 编程->动态生成和编译以多种语言表示的源代码