日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
簡(jiǎn)單介紹編譯C#代碼

編譯C#代碼應(yīng)用場(chǎng)景:
還沒想出來會(huì)用到哪里。動(dòng)態(tài)的代碼由誰來寫?普通用戶我想有一定的困難。特別是有了像 IronPython 這樣更容易使用的動(dòng)態(tài)嵌入腳本。
1) 像 LINQPad 這樣的輔助開發(fā)工具
2) 實(shí)現(xiàn)腳本引擎?
3) 探討...

主要使用命名空間 Microsoft.CSharp 編譯C#代碼,然后使用 CodeDom 和 反射調(diào)用,我這里寫了一個(gè)測(cè)試工具,看代碼:

 
 
 
  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;        // 用于反射調(diào)用   
  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 ErrorMessage = new List();  
  31.  
  32. public static bool Compile
    (string[] references, string source, string outputfile) {  
  33. // 編譯參數(shù)    
  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) { // 反射調(diào)用    
  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) {  // 列出編譯錯(cuò)誤  
  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. }  

作為演示,例子簡(jiǎn)單的規(guī)定類名必須是MyClass,必須有一個(gè)方法返回 string 類型的 GetResult 方法。以上介紹編譯C#代碼


分享文章:簡(jiǎn)單介紹編譯C#代碼
文章網(wǎng)址:http://www.5511xx.com/article/djheoco.html