网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#应用
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.给大家一个新的加密方法,C#的.
.C#几种常用的排序算法.
.C#实现大文件分块发送到客户端.
.使用c#捕获windows的关机事件.
.关于正则表达式匹配无异常资源耗.
.使用C#开发用户控制.
.病毒及流氓软件自我复制的简单实.
.捕捉摄相头的数据流 .
.使用C#创建SQL Server的存储过程.
.C#中判断字符串A中是否包含字符串.
.C#进制转换 的记录 .
.一个极其简单的在线C#IDE例子.
.C++与C#混合生成.NET程序 .
.汇总c#.net常用函数和方法集.
.分享动态生成文字图片解决方案.
.国外C#开源系统一览表.
.关于C#中的DateTime类型的细节问.
.将PUBS中的所有用户表内容分别用.
.C#中如何读写INI文件.
.如何用C#写所见即所得的设计器.

一个极其简单的在线C#IDE例子

发表日期:2008-5-12


  五一时去朋友那, 他问了个小问题, 只要写几十行代码就可以很好的说明问题.可偏偏机子没装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();
   }
  
   输出操作#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 code = @"
  using System;
  
  public class Program
  {
   public static void Main()
   {
   for(int index = 1;index <= 3;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]);
   }
   }
  }
 

 

上一篇:C#下提取汉字首字的拼音首字母并兼容英文与数字 人气:3431
下一篇:实用技巧:.Net框架类库中定时器类的使用 人气:2132
浏览全部C#的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐