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

推荐订阅源

Recent Commits to openclaw:main
Recent Commits to openclaw:main
U
Unit 42
WordPress大学
WordPress大学
Microsoft Azure Blog
Microsoft Azure Blog
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
Microsoft Security Blog
Microsoft Security Blog
T
The Blog of Author Tim Ferriss
博客园 - Franky
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
P
Palo Alto Networks Blog
NISL@THU
NISL@THU
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
C
Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Spread Privacy
Spread Privacy
小众软件
小众软件
T
Threat Research - Cisco Blogs
Project Zero
Project Zero
博客园 - 三生石上(FineUI控件)
D
Darknet – Hacking Tools, Hacker News & Cyber Security
The Register - Security
The Register - Security
The Hacker News
The Hacker News
F
Fortinet All Blogs
Security Latest
Security Latest
Cisco Talos Blog
Cisco Talos Blog
The GitHub Blog
The GitHub Blog
Stack Overflow Blog
Stack Overflow Blog
T
The Exploit Database - CXSecurity.com
量子位
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
P
Proofpoint News Feed
G
GRAHAM CLULEY
D
DataBreaches.Net
P
Privacy International News Feed
Y
Y Combinator Blog
Simon Willison's Weblog
Simon Willison's Weblog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
P
Proofpoint News Feed

博客园 - surfsky

用阿里的 sketch 插件 FusionDesign 来快速设计中后台 用 Sketch 设计和输出响应式网页 百度、高德、腾讯、天地图、谷歌、必应瓦片图地图切图工具 MapCutter(旧名 MapTiler),支持超大图、高清切片、webgl、leaflet、maptalk、openlayers、cesium netcore 下的 Javascript 表达式求值 中标麒麟安装SQLServer 中标麒麟上开启MySql 在 MAC 下配置 Nginx Color Schema 配色随笔 .Net与 WebAssembly 随笔 关于Xamarin、Qml、数据绑定、MVC、MVVM 相关的散讲 用Nuget部署程序包 Qt3D Qt3D Shader Qt QML 2D shader LearnOpenGL Qt3D 5.9 and future FineUI 相关 EntityFramwork 七七八八 Qt qml 单例模式
netcore 下的 C# 表达式求值
surfsky · 2020-05-19 · via 博客园 - surfsky

netframework 下的 codedom 代码已经不能用了,会报错,改用Roslyn来写:

先安装包

install-package Microsoft.CodeAnalysis.CSharp

核心代码,尝试了好久

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
public class CsEvaluator
{
        /// <summary>CSharp 表达式求值</summary>
        /// <param name="expression">CSharp 表达式。如:2.5, DateTime.Now</param>
        public object Eval(string expression)
        {
            // 代码
            var text = string.Format(@"
                using System;
                public class Calculator
                {{
                    public static object Evaluate() {{ return {0}; }}
                }}", expression);

            // 编译生成程序集
            var tree = SyntaxFactory.ParseSyntaxTree(text);
            var compilation = CSharpCompilation.Create(
                "calc.dll",
                new[] { tree },
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
                references: new[] { 
                    MetadataReference.CreateFromFile(typeof(object).Assembly.Location)
                });
            Assembly compiledAssembly;
            using (var stream = new MemoryStream())
            {
                var compileResult = compilation.Emit(stream);
                compiledAssembly = Assembly.Load(stream.GetBuffer());
            }

            // 用反射执行方法
            var calculatorClass = compiledAssembly.GetType("Calculator");
            var evaluateMethod = calculatorClass.GetMethod("Evaluate");
            return evaluateMethod.Invoke(null, null);
        }
}

可以快乐的测试了

var eval = new CsEvaluator();
var b = eval.Eval("5 > 4");
var d = eval.Eval("2.5");
var dt1 = eval.Eval("new DateTime(2018,1,1)");
var dt2 = eval.Eval("DateTime.Now");