c# 在线编译器c#,可以做成ide

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Microsoft.CSharp;

namespace CSharpScript
{
    public partial class formMain : Form
    {
        public formMain()
        {
            InitializeComponent();
            this.txtInput.Text = @"using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace MyApplication
{
    public class TestClass
    {
        static void Main(string[] args)
        {
            var str = ""www.haolizi.net 好例子网"";    
            Console.WriteLine(str);
        }
    }
}";
        }

        private void btnCompile_Click(object sender, EventArgs e)
        {
            string program=this.txtInput.Text;
            string output="";
            new Runner().CompileAndRun(program, out output);
            this.txtOutPut.Text = output;
        }

    }

    //public class CompileUtils
    //{
    //    public static bool CanCompile(string program)
    //    {
    //        return !Compile(program).Errors.HasErrors;
    //    }

    //    public static CompilerResults Compile(string program)
    //    {
    //        CompilerResults compilerResults = null;

    //        System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
    //        parameters.WarningLevel = 4;
    //        parameters.ReferencedAssemblies.Add("System.dll");
    //        parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");    
    //        //#if CompileIntoMemory
    //        parameters.GenerateInMemory = true;

    //        //new CompilerParameters(new string[]
    //        //    {
    //        //        "System.dll"
    //        //    })

    //        using (CSharpCodeProvider provider = new CSharpCodeProvider())
    //        {
    //            compilerResults = provider.CompileAssemblyFromSource(parameters,
    //            new string[] { program });
    //        }
    //        return compilerResults;
    //    }
    //}

    //public class PhoenixWriter : System.IO.TextWriter
    //{
    //    delegate void VoidAction();
    //    System.Windows.Forms.TextBox txtBox;

    //    public PhoenixWriter(System.Windows.Forms.TextBox box)
    //    {
    //        this.txtBox = box;
    //    }

    //    public override System.Text.Encoding Encoding
    //    {
    //        get { return System.Text.Encoding.UTF8; }
    //    }

    //    //here, must use parameter: char value
    //    public override void Write(char value)
    //    {
    //        VoidAction action = delegate
    //        {
    //            txtBox.AppendText(value.ToString());
    //        };
    //        txtBox.BeginInvoke(action);
    //    }
    //}


    public class Runner
    {
        private static readonly string[] ConsoleAssemblyNames = new[]
                                                                    {
                                                                        "mscorlib.dll", "Microsoft.CSharp.dll",
                                                                        "System.dll", "System.Core.dll",
                                                                        "System.Data.dll", "System.Data.DataSetExtensions.dll",
                                                                        "System.Xml.dll", "System.Xml.Linq.dll",
                                                                    };

        private static readonly Dictionary<string, string> CSharpCodeProviderOptions = new Dictionary<string, string>() { { "CompilerVersion", "v4.0" } };

        public bool CompileAndRun(string code, out string output)
        {
            var compiled = Compile(code);

            if (compiled.Errors.HasErrors)
            {
                output = ReturnErrors(compiled);
                return false;
            }
            output = ReturnOutput(compiled);
            return true;
        }

        private static string ReturnErrors(CompilerResults compiled)
        {
            var builder = new StringBuilder();
            builder.AppendLine("Compilation errors:");
            compiled.Errors.Cast<CompilerError>()
                .Select(ce => ce.ErrorText)
                .ToList()
                .ForEach(error => builder.AppendLine(error));
            return builder.ToString();
        }

        private static CompilerResults Compile(string code)
        {
            using (var csc = new CSharpCodeProvider(CSharpCodeProviderOptions))
            {
                var parameters = new CompilerParameters(ConsoleAssemblyNames) { IncludeDebugInformation = true, GenerateInMemory = true };
                var results = csc.CompileAssemblyFromSource(parameters, code);
                return results;
            }
        }

        private static string ReturnOutput(CompilerResults compiled)
        {
            
            using (var writer = new StringWriter())
            {
                Console.SetOut(writer);
                //Console.SetIn(new StringReader(""));
                var assembly = compiled.CompiledAssembly;

                Type mainType = assembly.GetTypes().First(x => x.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic).Count(m => m.Name.ToLower() == "main") > 0);

                var main = mainType.GetMethod("Main", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                var postedParams = new object[] { new string[]{} };
                main.Invoke(null, postedParams);
                return writer.ToString();
            }
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值