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

ASP.NET
ASP.NET在上传文件时对文件类型的高级判断的代码
JQuery运用ajax注册用户实例(后台asp.net)
Asp.net与SQLserver一起打包部署安装图文教程
asp.net 上传下载输出二进制流实现代码
asp.net(C#)解析Json的类代码
asp.net 截取字符串代码
asp.net ubb使用代码
asp.net XML文件操作实现代码
asp.net利用HttpModule实现防sql注入
ASP.NET(C#)中操作SQLite数据库实例
asp.net(c#)ref,out ,params的区别
asp.net(C#)防sql注入组件的实现代码
asp.net FCKeditor自定义非空验证
Asp.net TreeView来构建用户选择输入的方法 推荐
asp.net(C#)函数对象参数传递的问题
Asp.net中的GridView导出遇到的两个问题和解决方法
asp.Net 中获取一周第一天,一月第一天等实现代码
asp.net MaxLengthValidator 最大长度验证控件代码
C# 通用文件上传类
asp.net 自定义控件实现无刷新上传图片,立即显示缩略图,保存图片缩略图

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 62 ::
收藏到网摘: 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 编程->动态生成和编译以多种语言表示的源代码