- 热门文章:
- · 一次重构导向设计模式的实践(.NET)
- · 在设计期跟踪代码 (.NET)
- · 编译自己的资源文件编辑器(reseditor.exe)
- · .net辅助工具列表
- · .NET 2.0 基础类库中的范型——其他范型类
- · .NET 2.0 基础类库中的范型——Functional Programming
- · .NET 2.0 基础类库中的范型——范型集合
- · .NET 下的remoting使用。(TCP通道)
- · .NET系统学习----Globalization & Resources
- · CLR 调试接口的架构与应用 [3] 调试事件
- · CLR 调试接口的架构与应用 [2] 调试框架
- · .net事务处理并发性处理的意义(此文仅适合入门者阅读)
上一篇:收集了一些小技巧的连接 (.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 编程->动态生成和编译以多种语言表示的源代码
下一篇:一次重构导向设计模式的实践(.NET) >>
相关文章:
- · 在.Net1.2中对Xquery的支持
- · 第十五章 接口[《.net框架程序设计》读书笔记]
- · 第十四章 数组[《.net框架程序设计》读书笔记]
- · 第十三章 枚举类型与位标记[《.net框架程序设计》读书笔记]
- · 第十二章 委托[《.net框架程序设计》读书笔记]
- · 第十一章 事件[《.net框架程序设计》读书笔记]
- · 第十章 属性[《.net框架程序设计》读书笔记]
- · 第九章 方法[《.net框架程序设计》读书笔记]
- · 第八章 常数与字段[《.NET框架程序设计》读书笔记]
- · 第七章 类型成员及其访问限定[《.NET框架程序设计》读书笔记]
- · 手工修改动网新闻.net3.0的Bug
- · .net的MSMQ异步调用
- · 关于《.net框架程序设计》读书笔记
- · 前言[《.net框架程序设计》读书笔记]
- · 发布一个通用面板程序(htc)
- · .Net 实现纳秒级别计算
- · 快速理解.NET Framework[翻译]
- · .NET框架程序设计读书笔记(三)--.net框架类库(FCL)和通用类型系统、代码互操作
- · NET框架程序设计读书笔记(三)--执行程序集代码
- · 动态加载树----treeView
- · 在Internet上用通过.Net Remoting机制实现服务器对客户端的直接调用
- · .NET中的强名称机制
- · .NET中的GAC
- · .net框架程序设计读书笔记二(Microsoft .net 框架开发平台体系架构)
- · .net 框架程序设计 读书笔记(一)---.net 平台构成
- · .NET中的版本号
- · Win32类型和.net类型的对应表
- · 实战 .Net 数据访问层 - 1
- · 基于.Net的AOP实现技术
- · 非常了不起的工具iNET----------帮你实现.NET项目跨平台运行.
- · 服务器端异步 Web 方法
- · Prototype设计模式的实现
- · 用WinDbg探索CLR世界 [3] 跟踪方法的 JIT 过程
- · .net+oracle+crystalReports开发web应用程序学习笔记(二)
- · 使用.net Remtoing进行并行计算
- · 我的O/R Mapping实际开发经验之谈(二)
- · 走近COM Interop——RCW入门
- · 基于Grove的.NET应用程序开发提示
