成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

簡單介紹編譯C#代碼

開發 后端
本文介紹編譯C#代碼,主要使用命名空間 Microsoft.CSharp 編譯C#代碼,然后使用 CodeDom 和 反射調用,我這里寫了一個測試工具

編譯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#代碼

【編輯推薦】

  1. C#反射方法學習總結
  2. 淺談C#測量cpu性能
  3. C#遠程計算機的一些理論知識
  4. 淺析C# Static修飾
  5. C#轉換農歷的簡單方法
責任編輯:佚名 來源: Infoq
相關推薦

2009-09-01 10:35:19

C# 3.0編譯器

2009-08-21 17:55:52

C#復合控件

2009-08-07 17:41:40

C#預處理

2009-08-27 10:19:22

C#匿名類型

2009-08-06 14:53:41

C# User類

2009-08-18 17:37:57

C#固定指針

2009-08-10 16:19:37

C#冒泡排序

2009-08-14 17:27:56

C#方法參數

2009-08-06 18:15:13

C# SQL Serv

2009-09-01 16:19:57

C# new()約束

2009-09-03 09:40:57

C#創建表單

2009-08-20 16:25:59

C# 匿名方法

2009-08-25 13:38:35

C# Timer組件

2009-08-07 17:12:07

C# DLL函數

2009-08-03 17:51:43

C#引用類型

2009-08-14 16:46:44

C#元數據

2009-08-31 18:24:26

編譯C#文件

2009-08-27 16:29:18

C#動態編譯

2009-08-25 14:42:41

由C++轉向C#

2009-08-07 09:29:22

C#數組C#函數
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久aⅴ乱码一区二区三区 亚洲欧美综合精品另类天天更新 | 久久逼逼 | 国产男女视频 | 中文字幕视频在线观看 | 久久综合九色综合欧美狠狠 | 欧美日韩最新 | 在线观看国产视频 | 99精品国产一区二区三区 | 韩日精品在线观看 | 国产精品毛片一区二区三区 | 亚洲精品性视频 | 精品av | 欧美一级免费片 | av黄色免费在线观看 | 国产精品视频一区二区三区 | 超碰天天 | 激情麻豆视频 | 中文字幕在线免费视频 | 黄色片视频免费 | 99热都是精品 | 亚洲一区二区三区在线 | 男女在线网站 | 日韩在线不卡 | 国产精品大全 | 一区二区三区国产好的精 | 日韩久久成人 | 国产一区二区精品在线 | 在线免费观看成年人视频 | 亚洲一区二区三区在线播放 | 中文字幕第一页在线 | 成人激情视频免费在线观看 | 国产乱码精品一区二区三区av | 国产精品久久久久久妇女 | 日日天天| 国产色99精品9i | 在线国产视频 | 中文字幕在线人 | 日韩av中文 | 九九免费观看视频 | 日韩午夜激情 | 亚洲一区播放 |