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

ASP.NET
Active Directory 对象的控制
在vb中动态加载水晶报表rpt文件的方法
webconfig中进行登陆的权限、修改2
DataGrid(WinForm)显示行号最简单的方法
在EXCEL中获取列中不重复的值的个数
使用Visual C#制作可伸缩个性化窗体
关于控件注册和使用许可问题的解决办法
获取闭合符号中的字符串
判断点与多边形的状态(位置)
NHibernate初试
GetTickCount()函数精确到多少毫秒
VB.net中HOOK的应用(一)
C# to VB.Net translator..
用Visual C#调用Windows API函数
用API函数实现切换VCD的左右声道
元数据--自定义属性(VB.NET)
初学VB.NET连接SQL数据库
简单的DataGrid多表头制作方法
带输出参数的存储过程的使用及在C#中调用问题
VS的控件真是不好用,好不容易才搞定DataGrid

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


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