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

ASP.NET
使用C# 开发掩码输入文本框
点击DataGrid的列标头在DataGrid最后一行显示该列的和
ASP.NET之Web打印-终极解决篇
SQL Server 2000 Reporting Services: 怎样根据用户的语言偏好显示本地化的信息
利用底层键盘钩子拦载任意按键(回调版)
如何禁止调整自定义控件的尺寸?
用VB6.0编写磁盘格式化程序
Aspx中导Excel
ASP.NET组件设计Step by Step(3)
下面真正开始讲事件的内容
如何有效的使用C#读取文件
如何在C#中加载自己编写的动态链接库(DLL)
Managed DirectX 相关(DirectX.Capture Class Library && DirectShow.NET)
XQuery Reference-from w3schools.com
[译]Visual Basic 2005在语言上的增强(十三)显式的数组范围及小结
lucene的首次应用
[VBA]在后台删除工作表后出现的怪问题
VB.NET 数据库查询 [SQL字符串的生成]
JavaScript调用服务器事件
在Window2003上执行System.Diagnostics.Process.GetProcessesByName等方法失败的原因

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


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