nreco/lambdaparser
关于
用于运行时解析字符串表达式(公式、方法调用)。构建动态 LINQ 表达式树,并将其编译为 lambda 委托。
开发工具实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文NReco LambdaParser
Runtime parser for string expressions (formulas, method calls, properties/fields/arrays accessors). LambdaParser builds dynamic LINQ expression tree and compiles it to the lambda delegate. Types are resolved at run-time like in dynamic languages.
| NuGet | Windows x64 | Linux |
|---|---|---|
- can be used in any .NET app: net45 (legacy .NET Framework apps), netstandard1.3 (.NET Core apps), netstandard2.0 (all modern .NET apps).
- any number of expression arguments (values can be provided as dictionary or by callback delegate)
- supports arithmetic operations (+, -, *, /, %), comparisons (==, !=, >, <, >=, <=), conditionals including (ternary) operator ( boolVal ? whenTrue : whenFalse )
- access object properties, call methods and indexers, invoke delegates
- dynamic typed variables: performs automatic type conversions to match method signature or arithmetic operations
- create arrays with C#-like or JSON syntax:
new []{ 1, 2, 3}ornew [1, 2, 3]or just[1, 2, 3] - create dictionaries (key-value map) with C#-like or JSON syntax:
new dictionary{ {"a", 1}, {"b", 2} }ornew {"a":1}or{"a":1, "b":2} - local variables that may go before main expression:
var a = 5; var b = contextVar/total*100;(disabled by default, to enable useLambdaParser.AllowVarsproperty)
Nuget package: NReco.LambdaParser
var lambdaParser = new NReco.Linq.LambdaParser();
var varContext = new Dictionary<string,object>();
varContext["pi"] = 3.14M;
varContext["one"] = 1M;
varContext["two"] = 2M;
varContext["test"] = "test";
Console.WriteLine( lambdaParser.Eval("pi>one && 0<one ? (1+8)/3+1*two : 0", varContext) ); // --> 5
Console.WriteLine( lambdaParser.Eval("test.ToUpper()", varContext) ); // --> TEST
(see unit tests for more expression examples)
Custom values comparison
By default LambdaParser uses ValueComparer for values comparison. You can provide your own implementation or configure its option to get desired behaviour:
ValueComparer.NullComparisondetermines how comparison withnullis handled. 2 options:MinValue: null is treated as minimal possible value for any type - like .NET IComparerSql: null is not comparable with any type, including another null - like in SQL
ValueComparer.SuppressErrorsallows to avoid convert exception. If error appears during comparison exception is not thrown and this means that values are not comparable (= any condition leads tofalse).
var valComparer = new ValueComparer() { NullComparison = ValueComparer.NullComparisonMode.Sql };
var lambdaParser = new LambdaParser(valComparer);
Caching Expressions
The UseCache property determines whether the LambdaParser should cache parsed expressions. By default, UseCache is set to true, meaning expressions are cached to improve performance for repeated evaluations of the same expression.
Therefore, using a singleton instance of LambdaParser is recommended, rather than creating a new instance each time.
You can disable caching by setting UseCache to false if you want to save memory, especially when evaluating a large number of unique expressions.
var lambdaParser = new LambdaParser();
lambdaParser.UseCache = false;
Who is using this?
NReco.LambdaParser is in production use at SeekTable.com and PivotData microservice (used for user-defined calculated cube members: formulas, custom formatting).
License
Copyright 2016-2025 Vitaliy Fedorchenko and contributors
Distributed under the MIT license
探索相关项目
加载中…