首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> C# >> 正文

动态执行C#代码

  写这篇笔记的理由是因为上篇g提高的工具LINQPad,见:
  想用LINQ思考吗?扔掉SQL查询分析器,Come on LINQPad!
  该工具的实现就有执行动态代码的应用。

  应用场景:
  还没想出来会用到哪里。动态的代码由谁来写?普通用户我想有一定的困难。
  特别是有了像 IronPython 这样更容易使用的动态嵌入脚本。

  1) 像 LINQPad 这样的辅助开发工具
  2) 实现脚本引擎?
  3) 探讨...

  主要使用命名空间 Microsoft.CSharp 编译C#代码,然后使用 CodeDom 和 反射调用,我这里写了一个测试工具,看代码:

 

1. using System; 2. using System.Collections.Generic; 3. using System.ComponentModel; 4. using System.Drawing; 5. using System.Windows.Forms; 6. using System.CodeDom.Compiler; 7. using Microsoft.CSharp; // 用于编译C#代码 8. using System.Reflection; // 用于反射调用 9. 10. namespace CodeDomLearn 11. { 12. public partial class Form1 : Form 13. { 14. public Form1() { 15. InitializeComponent(); 16. 17. } 18. 19. private void button1_Click(object sender, EventArgs e) { 20. CodeCompiler.Compile(new string[] { }, textBox1.Text, ""); 21. listBox1.Items.Clear(); 22. foreach (string s in CodeCompiler.ErrorMessage) { 23. listBox1.Items.Add(s); 24. } 25. listBox1.Items.Add(CodeCompiler.Message); 26. } 27. } 28. 29. static class CodeCompiler { 30. static public string Message; 31. static public List<string> ErrorMessage = new List<string>(); 32. 33. public static bool Compile(string[] references, string source, string outputfile) { 34. // 编译参数 35. CompilerParameters param = new CompilerParameters(references, outputfile, true); 36. param.TreatWarningsAsErrors = false; 37. param.GenerateExecutable = false; 38. param.IncludeDebugInformation = true; 39. 40. // 编译 41. CSharpCodeProvider provider = new CSharpCodeProvider(); 42. CompilerResults result = provider.CompileAssemblyFromSource(param, new string[] { source }); 43. 44. Message = ""; 45. ErrorMessage.Clear(); 46. if (!result.Errors.HasErrors) { // 反射调用 47. Type t = result.CompiledAssembly.GetType("MyClass"); 48. if (t != null) { 49. object o = result.CompiledAssembly.CreateInstance("MyClass"); 50. Message = (string)t.InvokeMember("GetResult", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, o, null); 51. } 52. return true; 53. } 54. 55. foreach (CompilerError error in result.Errors) { // 列出编译错误 56. if (error.IsWarning) continue; 57. ErrorMessage.Add("Error(" + error.ErrorNumber + ") - " + error.ErrorText + "\t\tLine:" + error.Line.ToString() + " Column:"+error.Column.ToString()); 58. } 59. return false; 60. } 61. 62. } 63. }

 
  作为演示,例子简单的规定类名必须是MyClass,必须有一个方法返回 string 类型的 GetResult 方法。这是执行效果图:

 

【责编:Zenghui】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航