惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

P
Palo Alto Networks Blog
P
Proofpoint News Feed
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog
Google DeepMind News
Google DeepMind News
N
Netflix TechBlog - Medium
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
罗磊的独立博客
J
Java Code Geeks
月光博客
月光博客
F
Full Disclosure
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
U
Unit 42
WordPress大学
WordPress大学
A
About on SuperTechFans
C
Cyber Attacks, Cyber Crime and Cyber Security
SecWiki News
SecWiki News
Security Latest
Security Latest
C
Check Point Blog
C
CERT Recently Published Vulnerability Notes
小众软件
小众软件
I
InfoQ
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
B
Blog RSS Feed
V
Visual Studio Blog
博客园_首页
NISL@THU
NISL@THU
I
Intezer
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
The Register - Security
The Register - Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Latest news
Latest news
Project Zero
Project Zero
博客园 - 叶小钗
C
Cybersecurity and Infrastructure Security Agency CISA
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
博客园 - 【当耐特】
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
V
Vulnerabilities – Threatpost
博客园 - 司徒正美
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网

博客园 - 遗忘海岸

简协运动模拟 电磁场中的运动轨迹模拟 圆周运动模拟 带阻力的平抛运动 C# 队列的一些并发模拟 devexpress gridview master,detail视图 focuseRowHandle 同步选中 C# Tcp Server端实现,使用TcpListener 深信服务超融合管理Api调用 GDI+画工作流图的一些总结 绘制贝塞而曲线 GDI+画直线带箭头 devexpress schedulerControl Gantt View 使用 逻辑回归损失函数求导 matplotlim柱状图 匀加速运动模拟python,(matplotlib) C# 实现排列 python 类鸟群Boids 正太分布数据排序后分段数据的方差与标准差 Android 的一个通用列表单选AlertDialog封装
C#动态编译
遗忘海岸 · 2023-04-15 · via 博客园 - 遗忘海岸

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