簡易C#的IDE

來源:互聯網
上載者:User

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

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

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

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

代碼實現如下:

Code

[copy to clipboard]CODE:

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();
    }
  }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.