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

推荐订阅源

爱范儿
爱范儿
Forbes - Security
Forbes - Security
Help Net Security
Help Net Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
IT之家
IT之家
Microsoft Azure Blog
Microsoft Azure Blog
云风的 BLOG
云风的 BLOG
博客园 - 司徒正美
B
Blog
阮一峰的网络日志
阮一峰的网络日志
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
The Register - Security
The Register - Security
美团技术团队
C
CERT Recently Published Vulnerability Notes
I
Intezer
C
Cybersecurity and Infrastructure Security Agency CISA
Google Online Security Blog
Google Online Security Blog
B
Blog RSS Feed
PCI Perspectives
PCI Perspectives
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MyScale Blog
MyScale Blog
S
Securelist
Recorded Future
Recorded Future
Know Your Adversary
Know Your Adversary
Security Archives - TechRepublic
Security Archives - TechRepublic
M
MIT News - Artificial intelligence
C
Check Point Blog
T
Threat Research - Cisco Blogs
博客园 - Franky
P
Proofpoint News Feed
人人都是产品经理
人人都是产品经理
U
Unit 42
F
Fortinet All Blogs
S
Security @ Cisco Blogs
The GitHub Blog
The GitHub Blog
Apple Machine Learning Research
Apple Machine Learning Research
MongoDB | Blog
MongoDB | Blog
The Hacker News
The Hacker News
酷 壳 – CoolShell
酷 壳 – CoolShell
F
Full Disclosure
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
D
Docker
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LINUX DO - 热门话题

博客园 - 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");