• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

实现C#即时编译器

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

结合控制台重定向和C#即使编译器(见我上面两篇文章)写的WinForm即时C#编译器,功能还不错。
文本框就是你Main方法内的语句,可以输入任意测试代码,支持错误行号定位,编译结果捕获,自动拆分窗格等,程序按F5执行,F5…… 忘记在代码里面加说明了:( 时间不早了,上传睡觉,带批处理build代码和SharpDevelop方式源代码哦。

可以拿下面的代码测试程序

 

//F5: Compile and Run the app    F6:Kill current Running App
//-----------------------------------------------------------
System.Diagnostics.Process.Start("notepad");

//test Generic Collection
List<int> lstInt = new List<int>{
    1,3,5,7,9,11
};
foreach(var v in lstInt)
    Console.WriteLine(v);

//test Timer
System.Timers.Timer timer = new System.Timers.Timer(1000);
timer.Elapsed += delegate{
    System.Console.WriteLine(DateTime.Now.TimeOfDay.ToString());
};
timer.Start();
Console.ReadKey();

//test WinForm Application
//Application.Run(new Form());

 

这个程序用了我两天的时间,涉及到技术有:
1、同线程的控制台重定向到文本框
2、不同线程的控制台重定向到文本框-异步方式
3、编译代码并执行(Invoke方式和Process方式)
4、结束进程树
5、文件操作等

实现编译器的代码如下:

 

//#define CompileIntoMemory

using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace PhoenixCompiler
{
    /// <summary>
    
/// director the Console output to ListBox
    
/// in MainForm Construction: 
    
/// </summary>
    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);
        }
    }
    
    
    
    /// <summary>
    
/// Description of PhoenixCompiler.
    
/// </summary>
    public class PhoenixCompiler
    {
        public PhoenixCompiler()
        {
        }
        
        public string GenerateCode(string userCode)
        {
            string namespaces =@"using System;
using System.ComponentModel;
using System.Text;using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms
;using System.Threading;
using System.Reflection;
";
            string codeHead = @"
sealed class Phoenix
{
    public static void Main()
    {
//----------your code here-----------
";
        //userCode here
            string codeTail = @"
//----------your code here-----------
    }
}
";
            string compileCodes = namespaces + codeHead + userCode + codeTail;
            return compileCodes;
        }
        
        public CompilerResults Compile(string codes)
        {
            //CSharpCodeProvider
            System.CodeDom.Compiler.CodeDomProvider compiler = new Microsoft.CSharp.CSharpCodeProvider();
            //CompilerParameters
            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;
            #else
            parameters.GenerateExecutable=true;
            #endif        
            return compiler.CompileAssemblyFromSource(parameters,codes);
    
        }        
}
    
    
    /// <summary>
    
/// Description of MainForm.
    
/// </summary>
    public partial class MainForm : Form
    {
        System.Diagnostics.Process process;
        
        public MainForm()
        {
            InitializeComponent();
            //redirector Console output to ListBox lstResult
            Console.SetOut(new PhoenixWriter(txtResult));
        }
        
        //KillProcessTreeById
        public void KillProcessTreeById(int parentId)
        {
            //find all the process id created by parentId, Saved it to List childIds
            System.Collections.Generic.List<int> childIds=
                new System.Collections.Generic.List<int>();
            System.Diagnostics.Process[] processes =
                System.Diagnostics.Process.GetProcesses();
            foreach (var element in processes) {
                string path = string.Format("win32_process.handle='{0}'",element.Id);
                using (var mo= new System.Management.ManagementObject(path)) {
                    try {
                        mo.Get();
                    } catch (System.Management.ManagementException me) {
                        throw me;
                    }
                    int id=Convert.ToInt32(mo["ParentProcessId"]);
                    if (id==parentId) {
                        childIds.Add(element.Id);
                    }
                }    
            }
            
            foreach (var element in childIds) {
                //Console.WriteLine(element);
                System.Diagnostics.Process processKill = 
                    System.Diagnostics.Process.GetProcessById(element);
                if (processKill.ProcessName !="conhost") {
                    processKill.Kill();
                }
                //processKill.CloseMainWindow();
                
//processKill.Close();
            }
            var processParent = System.Diagnostics.Process.GetProcessById(parentId);
            if (processParent !=null) {
                processParent.Kill();
            }
        }
        
        void TxtCodeKeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {//F5:116        e.KeyCode.ToString() + ":" + e.KeyValue.ToString();
            if (e.KeyValue == 116) {
                if (process!=null) {
                    this.KillProcessTreeById(process.Id);
                    process=null;
                }
                PhoenixCompiler compiler = new PhoenixCompiler();
                string code = compiler.GenerateCode(txtCode.Text);
                System.CodeDom.Compiler.CompilerResults result = compiler.Compile(code);
                ShowResult(result);
            }
            if (e.KeyValue==117) {//F6:117 here should terminate the process tree ... ...
                if (process != null) {
                    this.KillProcessTreeById(process.Id);
                    process=null;
                }
            }
        }
        
        public void ShowResult(System.CodeDom.Compiler.CompilerResults result)
        {
            txtResult.Text="";
            Console.WriteLine("Phoenix Compiler! \r\n\tCopyright by XUMINGHUI(2013)");
            Console.WriteLine("-----------------------------------------------\n");
            if (result.Errors.HasErrors) {
                Console.WriteLine("Build ERROR:\r\n");
                foreach (System.CodeDom.Compiler.CompilerError error in result.Errors) {
                    Console.WriteLine(error.Line + ":" + error.ErrorText);
                }
                return;
            }
            //build OK
            Console.WriteLine("Build Successfully:\r\n");
            
            try {//call Main entry, Console output --> WinForm Control
                #if CompileIntoMemory
                Type Phoenix = result.CompiledAssembly.GetType("Phoenix");
                Phoenix.GetMethod("Main").Invoke(null,new string[]{});
                #else    //start another thread to run the generated application
                System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo{
                    FileName=result.PathToAssembly,
                    UseShellExecute=false,
                    CreateNoWindow=true,
                    RedirectStandardError=true,
                    RedirectStandardOutput=true
                };
                process = new System.Diagnostics.Process();
                process.OutputDataReceived +=(s,e)=>{
                    if (e.Data!=null) {
                        Console.WriteLine(e.Data);
                    }
                };
                process.ErrorDataReceived +=(s,e)=>{
                    if (e.Data!=null) {
                        Console.WriteLine(e.Data);
                    }
                };
                process.StartInfo = info;
                process.EnableRaisingEvents = true;
                process.Start();
                process.BeginOutputReadLine();
                process.BeginErrorReadLine();
                #endif
            } catch (System.Reflection.TargetInvocationException ex) {
                Console.WriteLine("Exception:" + ex.InnerException.Message);
            }
        }            

        
        void MainFormFormClosing(object sender, FormClosingEventArgs e)
        {
            if (process != null) {
                    this.KillProcessTreeById(process.Id);
                    process=null;
            }
        }
        
        
        void TxtResultDoubleClick(object sender, EventArgs e)
        {
            if (txtResult.Text=="") {
                return;
            }
            string path = System.IO.Path.GetTempFileName();
            System.IO.File.AppendAllText(path,txtResult.Text);
            System.Diagnostics.Process.Start("notepad",path);
        }

        void TxtCodeDoubleClick(object sender, EventArgs e)
        {
            PhoenixCompiler compiler = new PhoenixCompiler();
            string code = compiler.GenerateCode(txtCode.Text);
            string path = System.IO.Path.GetTempFileName();
            System.IO.File.AppendAllText(path,code);
            System.Diagnostics.Process.Start("notepad",path);
        }
    }
}

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#数组和集合整理发布时间:2022-07-10
下一篇:
c#利用定时器自动备份数据库(mysql)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap