c#-程序集-动态加载和创建程序集

using System;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
using System.Drawing;

namespace ConsoleApp29
{
    class Program : System.Windows.Forms.Form
    {
        EventHandler eventHandler;
        public Program()
        {
            eventHandler = OnClick1;
            lb1 = new Label();
            textBox = new TextBox();
            textBox.Location = new System.Drawing.Point(0, 25);
            textBox.Multiline = true;
            textBox.Width = 200;
            textBox.Height = 100;
            textBox2 = new TextBox();
            textBox2.Location = new System.Drawing.Point(0, 130);
            textBox2.Multiline = true;
            textBox2.Width = 200;
            textBox2.Height = 100;

            lb1.Text = "fff";
            button1 = new Button();
            button1.Location = new System.Drawing.Point(200, 5);
            button1.Text = "addd";
            button1.Click += eventHandler;
            this.Controls.Add(lb1);
            this.Controls.Add(button1);
            this.Controls.Add(textBox);
            this.Controls.Add(textBox2);
        }
        void OnClick1(object sender, EventArgs e)
        {
            lb1.Text = "变";
            var driver = new CodeDriver();
            bool isError;
            textBox2.Text = driver.CompileAndRun(textBox.Text, out isError);
            if (isError) {
                textBox2.BackColor = Color.Red;
            }
        }
        private Button button1;
        private Label lb1;
        private TextBox textBox;
        private TextBox textBox2;
        

        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("ddd");
            Application.Run(new Program());

            Console.ReadLine();
        }
    }
    
    class CodeDriver {
        private string prefix =
            "using System;" +
            "public static class Driver" +
            "{" +
            "public static void Run(){";
        private string postfix =
            "}" +
            "}";
        string getcode()
        {
            string str = "using System;" +
    "public static class Driver" +
        "{" +
            "public static void Run()" +
            "{" +
                "Console.WriteLine(\"Hello World\");" +
            "}" +
        "}";
            return str;
        }
        string getCode2() {
            return prefix + "Console.WriteLine(\"Hello World\");" + postfix;
        }
        public string CompileAndRun(string input,out bool hasError)
        {
            hasError = false;
            string returnData = null;
            CompilerResults results = null;
            using (var provider = new CSharpCodeProvider()) {
                var options = new CompilerParameters();
                options.GenerateInMemory = true;

                var sb = new StringBuilder();
                sb.Append(prefix);
                sb.Append(input);
                sb.Append(postfix);
                // compile:编译 Assembly:集合 Source:源代码
                results = provider.CompileAssemblyFromSource(options, sb.ToString());
                //results = provider.CompileAssemblyFromSource(options, getCode2());
            }
            if (results.Errors.HasErrors)
            {
                hasError = true;
                var errorMessage = new StringBuilder();
                foreach (CompilerError error in results.Errors) {
                    errorMessage.AppendFormat("{0}{0}", error.Line, error.ErrorText);
                    returnData = errorMessage.ToString();
                }
            }
            else {
                TextWriter temp = Console.Out;
                var writer = new StringWriter();
                Console.SetOut(writer);
                Type driverType = results.CompiledAssembly.GetType("Driver");
                driverType.InvokeMember("Run", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public,
                    null, null, null);
                Console.SetOut(temp);
                returnData = writer.ToString();

            }
            return returnData;
        }

    }
}

c#-程序集-动态加载和创建程序集_第1张图片

你可能感兴趣的:(c#)