- 浏览: 33953 次
最新评论
一个极其简单的在线C#IDE例子(zz)
19小时前
五一时去朋友那, 他问了个小问题, 只要写几十行代码就可以很好的说明问题.可偏偏机子没装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
{
///
/// 自定义的输出类
///
public class Consoler
{
//存储所有输出
public static Dictionary Outputs { get; set; }
static Consoler()
{
Outputs = new Dictionary();
}
输出操作
}
}
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 code = @"
using System;
public class Program
{
public static void Main()
{
for(int index = 1;index
{
Console.Write(index);
}
}
}
";
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]);
}
}
}
然后只要搬到Web上就可以了, 截图:
完整程序下载 : VSOnline.rar
发表评论
-
C#基础语法总结
2012-01-20 10:59 586C#基础语法总结 2012年01月14日 第一部分 基 ... -
C段错误总结
2012-01-20 10:59 704C段错误总结 2012年01月07日 最近一段时间在li ... -
C语言
2012-01-20 10:58 753C语言 2011年10月02日 ... -
c#日期格式转换大全-望穿秋水-博客园
2012-01-20 10:58 1479c#日期格式转换大全-望 ... -
疑难问题 HTML Parsing Error解决方案
2012-01-19 15:54 5945疑难问题 HTML Parsing Error解决方案 20 ... -
[main] python 6140 C:\cygwin\bin\python.exe: *** fatal error - unable to remap
2012-01-19 15:54 1306[main] python 6140 C:\cygwin\bi ... -
Unix网络编程代码 第16章 非阻塞式I/O
2012-01-19 15:54 1045Unix网络编程代码 第16章 非阻塞式I/O 2012年0 ... -
Linux与Android休眠唤醒对比(一)
2012-01-19 15:54 1280Linux与Android休眠唤醒对比(一) 2012年01 ... -
牵着ta的手一起走
2012-01-17 05:39 584牵着ta的手一起走 2011 ... -
陈慧琳是女性的心灵鸡汤
2012-01-17 05:39 660陈慧琳是女性的心灵鸡汤 2011年11月25日 ... -
北京自考专科接考本科要分清专业类别
2012-01-17 05:39 753北京自考专科接考本科 ... -
新目标英语八年级上册unit9 sectionA 1a-grammar导学案
2012-01-17 05:39 726新目标英语八年级上册unit9 sectionA 1a-gra ... -
符合用户体验是获得好的关键词排名的关键
2012-01-17 05:39 663符合用户体验是获得好 ... -
普贤行愿品讲记第七卷
2012-01-16 04:33 567普贤行愿品讲记第七卷 ... -
黄帝内经篇
2012-01-16 04:33 544黄帝内经篇 2012年01月04日 [b][/b] -
《中国不高兴》与王小波
2012-01-16 04:33 551《中国不高兴》与王小 ... -
回朋友:修行的概念一定要正确!理通就通了百分之七(1--10)
2012-01-16 04:33 630回朋友:修行的概念一定 ... -
【转】 通灵居士果卿谈自己诵大悲咒的神奇感应
2012-01-16 04:33 13251【转】 通灵居士果卿谈 ...
相关推荐
如果在本地电脑没有C#的编译器,可以直接在线编程,非常方便!
可以实现简单的在线编辑并运行C#程序
一个轻量级的C# IDE,用于做小程序极为合适.
C#例子C#例子C#例子C#例子C#例子C#例子C#例子C#例子C#例子C#例子C#例子C#例子C#例子
C#、VB.net开源IDE,小巧的.net开发平台
三个C#线程的例子三个C#线程的例子三个C#线程的例子三个C#线程的例子
c#100例子c#100例子c#100例子c#100例子c#100例子c#100例子c#100例子,对初学者很有帮助的
适合新手学习练习的29个例子,c#基础例子,上课必备29个例子
c#示例,一个很简单的例子 c#示例,一个很简单的例子 c#示例,一个很简单的例子 c#示例,一个很简单的例子 c#示例,一个很简单的例子 c#示例,一个很简单的例子
100个c#小例子 压缩包 初学者看看很有帮助
C# IDE Mobile v12.0 绿色英文版 此程序需.NET2.0支持才能运行(一般WM6及以上都集成NET2.0了) 一款在手机上开发运行C#程序代码的平台工具
c#及ASPNET例子c#及ASPNET例子c#及ASPNET例子c#及ASPNET例子
C#例子100个经典例子,代码
MVC简单例子C#,简单易懂 MVC简单例子C#,简单易懂 MVC简单例子C#,简单易懂
c#程序设计例子c#程序设计例子c#程序设计例子c#程序设计例子
C#串口操作一个很好的例子C#源代码,using System.Runtime.InteropServices;
C#的一个串口程序。协议就不说了,删除协议之后就是个基本的串口通信例子了,和大家分享一下
IOC模式 c#经典例子 IOC 例子 IOC模式 c#经典例子 IOC 例子 IOC模式 c#经典例子 IOC 例子
学习C#的例子,初级的学习。 介绍了MVC的运行原理,机制。 对控制器,试图,数据模型的使用都有说明