【xiaosonl】一個極其簡單的線上C#IDE例子

iDotNetSpace發表於2008-06-10

線上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<string, Consoler> Outputs getset; }

        
static Consoler()
        
{
            Outputs 
= new Dictionary<string, Consoler>();
        }


        
輸出操作
    }

}

 

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 getset; }

        
/// 
        
/// 建構函式
        
/// 
        
/// Framework版本,可選擇v2.0, v3.0, v3.5

        public CodeRun(string compilerVersion)
        
{
            CompilerVersion 
= compilerVersion;
        }


        
/// 
        
/// 建構函式,預設為3.5版本
        
/// 

        public CodeRun()
        
{
            CompilerVersion 
= "v3.5";
        }


        
/// 
        
/// 動態編譯並執行程式碼
        
/// 
        
/// 程式碼
        
/// 返回輸出內容

        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<stringstring>() 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-343230/,如需轉載,請註明出處,否則將追究法律責任。

相關文章