五一時去朋友那, 他問了個小問題, 只要寫幾十行代碼就可以很好的說明問題.可偏偏機子沒裝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
{
/**//// <summary>
/// 自定義的輸出類
/// </summary>
public class Consoler
{
//存儲所有輸出
public static Dictionary<string, Consoler> Outputs { get; set; }
static Consoler()
{
Outputs = new Dictionary<string, Consoler>();
}
輸出操作#region 輸出操作
//當前輸出
public List<string> Output { get; private set; }
public Consoler()
{
Output = new List<string>();
}
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
{
/**//// <summary>
/// 代碼執行類
/// </summary>
public class CodeRun
{
/**//// <summary>
/// Framework版本,可選擇v2.0, v3.0, v3.5
/// </summary>
private string CompilerVersion { get; set; }
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="compilerVersion">Framework版本,可選擇v2.0, v3.0, v3.5</param>
public CodeRun(string compilerVersion)
{
CompilerVersion = compilerVersion;
}
/**//// <summary>
/// 構造函數,默認為3.5版本
/// </summary>
public CodeRun()
{
CompilerVersion = "v3.5";
}
/**//// <summary>
/// 動態編譯並執行代碼
/// </summary>
/// <param name="code">代碼</param>
/// <returns>返回輸出內容</returns>
public List<string> 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<string, string>() { { "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<string> 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<string> expected = new List<string>() { "1", "2", "3" };
List<string> 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<string> actual;
actual = target.Run(code, "1", "System.Core.dll");
Assert.AreEqual("3", actual[0]);
}
}
}