遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

C#动态编译

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
using System.IO;
using System.Diagnostics;
namespace WindowsFormsApplication18
{
    public partial class Form2 : Form
    {
        private BI_Log _LogEnt = new BI_Log();
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _LogEnt.LogType = "错误";
            _LogEnt.Priority = 4;
            //MessageBox.Show("确定启动吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            var sw = new Stopwatch();
            sw.Start();
            Test();
            sw.Stop();
            Console.WriteLine(sw.ElapsedMilliseconds);

        }

        #region

        private void Test()
        {
            var code = @"
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.CodeDom.Compiler;
    using Microsoft.CSharp;
    using System.Reflection;
    using System.IO;
    using WindowsFormsApplication18;
    public class Abc {
       public bool Get(BI_Log ent) { return ent.Priority>3; }
    }
";

            string exePath = Assembly.GetExecutingAssembly().Location;
            string exeDir = Path.GetDirectoryName(exePath);

            AssemblyName[] assemRefs = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
            List<string> references = new List<string>();

            foreach (AssemblyName assemblyName in assemRefs)
                references.Add(assemblyName.Name + ".dll");

            for (int i = 0; i < references.Count; i++)
            {
                string localName = Path.Combine(exeDir, references[i]);

                if (File.Exists(localName))
                    references[i] = localName;
            }

            references.Add(exePath);

            //CompilerParameters compiler_parameters = new CompilerParameters(references.ToArray());


            var options = new CompilerParameters(references.ToArray());
            options.GenerateExecutable = false;
            options.GenerateInMemory = true;

            var provider = new CSharpCodeProvider();
            var compile = provider.CompileAssemblyFromSource(options, code);

            var type = compile.CompiledAssembly.GetType("Abc");
            var abc = Activator.CreateInstance(type);

            var method = type.GetMethod("Get");
            var objs = new object[] { _LogEnt};
            var result = method.Invoke(abc, objs);

            Console.WriteLine(result); //输出:abc
        }
        #endregion
    }
}
View Code

 

posted on 2023-04-15 08:11  遗忘海岸  阅读(18)  评论(0编辑  收藏  举报