ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 【xiaosonl】一个极其简单的在线C#IDE例子

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

原创 Linux操作系统 作者:iDotNetSpace 时间:2008-06-10 10:15:17 0 删除 编辑

在线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 { get; set; }

        
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 { get; set; }

        
/// 
        
/// 构造函数
        
/// 

        
/// 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<string, string>() 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-343230/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5766119