日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

一个简单的C#在线IDE示例

發布時間:2023/12/31 C# 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一个简单的C#在线IDE示例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


個簡單的C#在線IDE主要解決兩個問題:
???????????????????? 一是如何將網頁上文本框的代碼編譯并執行;
???????????????????? 二是如何將程序運行結果在網頁上輸出.

  第一個問題不難, dotNET已經有現成的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?
 ???????? ?string?code?=?@"?
 ????????? ???????????????using?System;?
  ?
 ????????? ???????????????public?class?Program?
 ????????? ???????????????{?
 ????????????????????????????  public?static?void?Main()?
  ??????????????????????????? {?
  ??????????????????????????????? ?for(int?index?=?1;index?<=?3;index++)?
  ???????????????????????????????? {?
  ???????????????????????????????????? ???Console.Write(index);?
  ?????????????????????????????????}?
  ???????????????????????????? }?
 ????????? ??????????? ????}??
";???//?多行?string結束

 ???????? ?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]);?
 ????? ?}?
  ?}?
}

轉載于:https://www.cnblogs.com/guangrou/archive/2008/06/02/1212197.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的一个简单的C#在线IDE示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。