微軟的官方套件之一
初次執行時需要花費一些時間編譯,因次有效能要求的需求,請建議不要使用
優點:
缺點:
評價:★★★★
使用說明請參考前一篇 的介紹
Install-Package CodingSeb.ExpressionEvaluator
建立 Expression Evaluator
using CodingSeb.ExpressionEvaluator; using EvaluateExpressionSample.Scripts; using System; using System.Collections.Generic; public static class ExpressionEvaluator { // 執行 Expression public static object Eval(string expression) { var evaluator = new ExpressionEvaluator(); // 設定 import 和 reference var namespaces = new List<string>() { "System", "System.Math", "System.Linq", "System.Text", "System.Text.RegularExpressions", "System.ComponentModel", "System.Collections", "System.Collections.Generic", "System.Collections.Specialized", "System.Globalization", "System.Security.Cryptography", }; evaluator.Namespaces = namespaces; // 執行 Expression return evaluator.Evaluate(expression); } // 執行 Script public static object RunScript(string script) { var evaluator = new ExpressionEvaluator(); // 設定 import 和 reference var namespaces = new List<string>() { "System", "System.Math", "System.Linq", "System.Text", "System.Text.RegularExpressions", "System.ComponentModel", "System.Collections", "System.Collections.Generic", "System.Collections.Specialized", "System.Globalization", "System.Security.Cryptography", }; evaluator.Namespaces = namespaces; // 執行 Script return evaluator.ScriptEvaluate(script); } }
使用
// result: "2021-04-06 10:00:00" object dateResult = ExpressionEvaluator.Eval("DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss\")"); // result: 178.53981633974485 object mathResult = ExpressionEvaluator.Eval("(5 * 5 * PI) + 100"); // result: "te" object textResult = ExpressionEvaluator.Eval("\"testsplit\".Split('s')[0]");
Install-Package Flee
建立 Expression Evaluator
using Flee.PublicTypes; using System; using System.Collections; using System.Text.RegularExpressions; public static class ExpressionEvaluator { public static object Eval(string expression) { var context = new ExpressionContext(); // 設定 import 和 reference context.Imports.AddType(typeof(Math), "Math"); context.Imports.AddType(typeof(DateTime), "DateTime"); context.Imports.AddType(typeof(TimeSpan), "TimeSpan"); context.Imports.AddType(typeof(Regex), "Regex"); context.Imports.AddType(typeof(RegexOptions), "RegexOptions"); context.Imports.AddType(typeof(IEnumerable), "List"); // 執行 Expression var generic = context.CompileGeneric<object>(expression); return generic.Evaluate(); } }
使用
// result: "2021-04-06 10:00:00" object dateResult = ExpressionEvaluator.Eval("DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss\")"); // result: 178.53981633974485 object mathResult = ExpressionEvaluator.Eval("(5 * 5 * PI) + 100"); // result: "te" object textResult = ExpressionEvaluator.Eval("\"testsplit\".Split('s')[0]");
Install-Package DynamicExpresso.Core
建立 Expression Evaluator
using DynamicExpresso; using System; public static class ExpressionEvaluator { public static object Eval(string expression) { var interpreter = new Interpreter(); // 設定 import 和 reference interpreter.Reference(typeof(Math)); interpreter.Reference(typeof(DateTime)); interpreter.Reference(typeof(TimeSpan)); interpreter.Reference(typeof(Regex)); interpreter.Reference(typeof(RegexOptions)); interpreter.Reference(typeof(IEnumerable)); // 執行 Expression return interpreter.Eval(expression); } }
使用
// result: "2021-04-06 10:00:00" object dateResult = ExpressionEvaluator.Eval("DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss\")"); // result: 178.53981633974485 object mathResult = ExpressionEvaluator.Eval("(5 * 5 * PI) + 100"); // result: "te" object textResult = ExpressionEvaluator.Eval("\"testsplit\".Split('s')[0]");
Roslyn C# Scripting | CodingSeb Expression Evaluator | Flee | Dynamic Expresso | |
---|---|---|---|---|
License | LGPL |
|||
Github Star | N/A | |||
Github Forks | N/A | |||
Support .NET Core | ✔️ | ✔️ | ✔️ | ✔️ |
Support new Keyword |
✔️ | ✔️ | ||
Variables | ✔️ | ✔️ | ✔️ | ✔️ |
Import Namespace | ✔️ | ✔️ | ✔️ | |
Reference Assembly | ✔️ | ✔️ | ✔️ | |
Pre-build Function | ✔️ | |||
評價 | ⭐️ ⭐️ ⭐️ ⭐️ | ⭐️ ⭐️ ⭐️ ⭐️ ⭐️ | ⭐️ ⭐️ ⭐️ ⭐️ | ⭐️ ⭐️ ⭐️ ⭐️ |
除了上述的做法,還有其他的做法
由於我沒有深入研究,請自行研究
Apache 2.0 License
SoftCircuits ExpressionEvaluator
MIT License