温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

编译C#代码的应用方法是什么

发布时间:2021-12-02 11:59:54 来源:亿速云 阅读:117 作者:iii 栏目:编程语言

这篇文章主要介绍“编译C#代码的应用方法是什么”,在日常操作中,相信很多人在编译C#代码的应用方法是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”编译C#代码的应用方法是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

编译C#代码应用场景:
还没想出来会用到哪里。动态的代码由谁来写?普通用户我想有一定的困难。特别是有了像 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. namespace CodeDomLearn  

  10. {  

  11. public partial class Form1 : Form  

  12. {  

  13. public Form1() {  

  14. InitializeComponent();  

  15.  

  16. }  

  17.  

  18. private void button1_Click(object sender, EventArgs e) {  

  19. CodeCompiler.Compile(new string[] { }, textBox1.Text, "");  

  20. listBox1.Items.Clear();  

  21. foreach (string s in CodeCompiler.ErrorMessage) {  

  22. listBox1.Items.Add(s);  

  23. }  

  24. listBox1.Items.Add(CodeCompiler.Message);  

  25. }  

  26. }  

  27.  

  28. static class CodeCompiler {  

  29. static public string Message;  

  30. static public List<string> ErrorMessage = new List<string>();  

  31.  

  32. public static bool Compile
    (string[] references, string source, string outputfile) {  

  33. // 编译参数    

  34. CompilerParameters param = new CompilerParameters
    (references, outputfile, true);  

  35. param.TreatWarningsAsErrors = false;  

  36. param.GenerateExecutable = false;  

  37. param.IncludeDebugInformation = true;  

  38.  

  39. // 编译    

  40. CSharpCodeProvider provider = new CSharpCodeProvider();  

  41. CompilerResults result = provider.CompileAssemblyFromSource
    (param, new string[] { source });  

  42.  

  43. Message = "";  

  44. ErrorMessage.Clear();  

  45. if (!result.Errors.HasErrors) { // 反射调用    

  46. Type t = result.CompiledAssembly.GetType("MyClass");  

  47. if (t != null) {  

  48. object o = result.CompiledAssembly.CreateInstance("MyClass");  

  49. Message = (string)t.InvokeMember("GetResult", BindingFlags.Instance | 
    BindingFlags.InvokeMethod | BindingFlags.Public, null, o, null);  

  50. }  

  51. return true;  

  52. }  

  53.  

  54. foreach (CompilerError error in result.Errors) {  // 列出编译错误  

  55. if (error.IsWarning) continue;  

  56. ErrorMessage.Add("Error(" + error.ErrorNumber + ") - " + error.ErrorText + 
    "\t\tLine:" + error.Line.ToString() + "  Column:"+error.Column.ToString());  

  57. }  

  58. return false;  

  59. }  

  60.  

  61. }  

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

到此,关于“编译C#代码的应用方法是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI