国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

一個極其簡單的在線C#IDE例子

2019-11-17 04:33:21
字體:
來源:轉載
供稿:網友

  五一時去朋友那, 他問了個小問題, 只要寫幾十行代碼就可以很好的說明問題.可偏偏機子沒裝VS, 只好做罷.回來后想想, 要是有個在線的C#IDE就好了.于是上網查了下相關的資料, 整出來個簡單的在線C#IDE.

  做這個,主要要解決兩個問題, 一是如果將網頁上文本框的代碼編譯并執行;二是如果將程序運行結果在網頁上輸出.

  第一個問題不難, .NET已經有現成的C#編譯類CSharpCodePRovider(或是其它語言的),再使用CompilerParameters類做為編譯參數,就可以很容易的實現.

  第二個問題, 舉最簡單情況, 就是將Console.Write方法輸出的內容在網頁上顯示出來.這其實也很好辦,只要在編譯之前, 在輸出語句做一個替換, 將輸出的內容存到另一個地方.等運行結束后, 再從那個地方取出來就是了.

  代碼實現如下: 以下是引用片段:
 using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace VSOnline.Framework
  {
   /**//// 
   /// 自定義的輸出類
   /// 
   public class Consoler
   {
   //存儲所有輸出
   public static Dictionary Outputs { get; set; }
  
   static Consoler()
   {
   Outputs = new Dictionary();
   }
  
   輸出操作#region 輸出操作
  
   //當前輸出
   public List Output { get; private set; }
  
   public Consoler()
   {
   Output = new List();
   }
  
   public void Write(object str)
   {
   Output.Add(str.ToString());
   }
  
   public void WriteLine(object str)
   {
   Output.Add(str.ToString() + "/n");
   }
  
   #endregion
   }
  }
  using System;
  using System.Reflection;
  using Microsoft.CSharp;
  using System.CodeDom.Compiler;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace VSOnline.Framework
  {
   /**//// 
   /// 代碼執行類
   /// 
   public class CodeRun
   {
   /**//// 
   /// Framework版本,可選擇v2.0, v3.0, v3.5
   /// 
   private string CompilerVersion { get; set; }
  
   /**//// 
   /// 構造函數
   /// 
   /// Framework版本,可選擇v2.0, v3.0, v3.5
   public CodeRun(string compilerVersion)
   {
   CompilerVersion = compilerVersion;
   }
  
   /**//// 
   /// 構造函數,默認為3.5版本
   /// 
   public CodeRun()
   {
   CompilerVersion = "v3.5";
   }
  
   /**//// 
   /// 動態編譯并執行代碼
   /// 
   /// 代碼
   /// 返回輸出內容
   public List Run(string code, string id, params string[] assemblies)
   {
   Consoler.Outputs.Add(id, new Consoler());
   CompilerParameters compilerParams = new CompilerParameters();
   //編譯器選項設置
   compilerParams.CompilerOptions = "/target:library /optimize";
   //compilerParams.CompilerOptions += @" /lib:""C:/Program Files/Reference Assemblies/Microsoft/Framework/v3.5/""";
   //編譯時在內存輸出
   compilerParams.GenerateInMemory = true;
   //生成調試信息
   compilerParams.IncludeDebugInformation = false;
   //添加相關的引用
   foreach (string assembly in assemblies)
   {
   compilerParams.ReferencedAssemblies.Add(assembly);
   }
   compilerParams.ReferencedAssemblies.Add("mscorlib.dll");
   compilerParams.ReferencedAssemblies.Add("System.dll");
   if (this.CompilerVersion == "v3.5")
   {
   compilerParams.ReferencedAssemblies.Add("System.Core.dll");
   }
  
   string path = "";
   try
   {
   path = HttpContext.Current.Server.MapPath("/bin/");
   }
   catch { }
  
   compilerParams.ReferencedAssemblies.Add(path + "VSOnline.Framework.dll");
   CSharpCodeProvider compiler = new CSharpCodeProvider(new Dictionary() { { "CompilerVersion", CompilerVersion } });
   //編譯
   code = code.Replace("Console.WriteLine", string.Format("VSOnline.Framework.Consoler.Outputs[/"{0}/"].WriteLine", id));
   code = code.Replace("Console.Write", string.Format("VSOnline.Framework.Consoler.Outputs[/"{0}/"].Write", id));
   CompilerResults results = compiler.CompileAssemblyFromSource(compilerParams, code);
   //錯誤
   if (results.Errors.HasErrors)
   {
   foreach (CompilerError error in results.Errors)
   {
   Consoler.Outputs[id].Output.Add(error.ErrorText + "/n");
   }
  
   return ReturnOutput(id);
   }
   //創建程序集
   Assembly asm = results.CompiledAssembly;
   //獲取編譯后的類型
   object mainClass = asm.CreateInstance("Program");
   Type mainClassType = mainClass.GetType();
   //輸出結果
   mainClassType.GetMethod("Main").Invoke(mainClass, null);
  
   return ReturnOutput(id);
   }
  
   private List ReturnOutput(string id)
   {
   string[] output = new string[Consoler.Outputs[id].Output.Count];
   Consoler.Outputs[id].Output.CopyTo(output, 0);
   Consoler.Outputs.Remove(id);
  
   return output.ToList();
   }
   }
  }
  
  測試:

   以下是引用片段:
using VSOnline.Framework;
  using Microsoft.VisualStudio.TestTools.UnitTesting;
  using System.Collections.Generic;
  using System;
  using FastDev.Core;
  using System.Linq;
  
  namespace Test
  {
   [TestClass()]
   public class CodeRunTest
   {
   [TestMethod()]
   public void RunTest()
   {
   CodeRun target = new CodeRun();
  
   string code = @"
  using System;
  
  public class Program
  {
   public static void Main()
   {
   for(int index = 1;index <= 3;index++)
   {
   Console.Write(index);
   }
   }
  }
   ";
   List expected = new List() { "1", "2", "3" };
   List actual;
   actual = target.Run(code, "1");
   Assert.AreEqual(true, expected.SerializeEqual(actual));
  
   actual = target.Run(code, "2");
   Assert.AreEqual(true, expected.SerializeEqual(actual));
   }
  
   [TestMethod()]
   public void Run35Test()
   {
   CodeRun target = new CodeRun();
  
   string code = @"
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Linq;
  
  public class Program
  {
   public static string Name { get; set; }
  
   public static void Main()
   {
   Name = ""3"";
   Console.Write(Name);
   }
  }
   ";
   List actual;
   actual = target.Run(code, "1", "System.Core.dll");
   Assert.AreEqual("3", actual[0]);
   }
   }
  }
 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宁海县| 卓尼县| 克什克腾旗| 信宜市| 昌邑市| 渝北区| 贡山| 老河口市| 高安市| 安徽省| 山丹县| 汾阳市| 离岛区| 东乌珠穆沁旗| 乌恰县| 荣成市| 镇康县| 哈巴河县| 墨脱县| 湘西| 新和县| 常德市| 巩义市| 汝阳县| 高平市| 修文县| 即墨市| 鲜城| 靖西县| 锡林郭勒盟| 海安县| 嘉黎县| 灵宝市| 韶关市| 赞皇县| 法库县| 敦煌市| 潮安县| 沛县| 阳山县| 抚宁县|