一个简单的C#在线IDE示例
生活随笔
收集整理的這篇文章主要介紹了
一个简单的C#在线IDE示例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
做一個簡單的C#在線IDE主要解決兩個問題:
???????????????????? 一是如何將網(wǎng)頁上文本框的代碼編譯并執(zhí)行;
???????????????????? 二是如何將程序運行結(jié)果在網(wǎng)頁上輸出.
第一個問題不難, dotNET已經(jīng)有現(xiàn)成的C#編譯類CSharpCodeProvider(或是其它語言的),再使用CompilerParameters類做為編譯參數(shù),就可以很容易的實現(xiàn).
第二個問題, 舉最簡單情況, 就是將Console.Write方法輸出的內(nèi)容在網(wǎng)頁上顯示出來.這其實也很好辦,只要在編譯之前, 在輸出語句做一個替換, 將輸出的內(nèi)容存到另一個地方.等運行結(jié)束后, 再從那個地方取出來就是了.
代碼實現(xiàn)如下:
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?輸出操作?
?
?????//當(dāng)前輸出?
???? ?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?
{?
?///??
?///?代碼執(zhí)行類?
?///??
?public?class?CodeRun?
?{?
??????///??
?????///?Framework版本,可選擇v2.0,?v3.0,?v3.5?
?????///??
?????private?string?CompilerVersion?{?get;?set;?}?
?
?????///??
?????///?構(gòu)造函數(shù)?
?????///??
?????///?Framework版本,可選擇v2.0,?v3.0,?v3.5?
?? ?public?CodeRun(string?compilerVersion)?
?{?
???? ?CompilerVersion?=?compilerVersion;?
?}?
?
?///??
?///?構(gòu)造函數(shù),默認(rèn)為3.5版本?
?///??
?public?CodeRun()?
?{?
???? ?CompilerVersion?=?"v3.5";?
?}?
?
?///??
?///?動態(tài)編譯并執(zhí)行代碼?
?///??
?///?代碼?
?///?返回輸出內(nèi)容?
?public?List?Run(string?code,?string?id,?params?string[]?assemblies)?
?{?
???? ?Consoler.Outputs.Add(id,?new?Consoler());?
???? ?CompilerParameters?compilerParams?=?new?CompilerParameters();?
???? ?//編譯器選項設(shè)置?
???? ?compilerParams.CompilerOptions?=?"/target:library?/optimize";?
???? ?//compilerParams.CompilerOptions?+=?@"?/lib:""C:\Program?Files\Reference?Assemblies\Microsoft\Framework\v3.5\""";?
???? ?//編譯時在內(nèi)存輸出?
???? ?compilerParams.GenerateInMemory?=?true;?
???????//生成調(diào)試信息?
???????compilerParams.IncludeDebugInformation?=?false;?
???? ?//添加相關(guān)的引用?
???? ?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);?
??????}?
???????//創(chuàng)建程序集?
?????Assembly?asm?=?results.CompiledAssembly;?
???????//獲取編譯后的類型?
???????object?mainClass?=?asm.CreateInstance("Program");?
???? ?Type?mainClassType?=?mainClass.GetType();?
???? ?//輸出結(jié)果?
???? ?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?
???????? ?string?code?=?@"?
????????? ???????????????using?System;?
?
????????? ???????????????public?class?Program?
????????? ???????????????{?
???????????????????????????? public?static?void?Main()?
??????????????????????????? {?
??????????????????????????????? ?for(int?index?=?1;index?<=?3;index++)?
???????????????????????????????? {?
???????????????????????????????????? ???Console.Write(index);?
?????????????????????????????????}?
???????????????????????????? }?
????????? ??????????? ????}??";???//?多行?string結(jié)束
???????? ?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]);?
????? ?}?
?}?
}
轉(zhuǎn)載于:https://www.cnblogs.com/guangrou/archive/2008/06/02/1212197.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的一个简单的C#在线IDE示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图片image和byte处理,fileu
- 下一篇: 妈的,混蛋 wfopen_s 函数