Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dahomey-technologies/dahomey.expressionevaluator
Evaluate C# Formulas at Runtime
https://github.com/dahomey-technologies/dahomey.expressionevaluator
csharp dotnet dotnet-standard evaluate evaluator expression expression-evaluator expression-parser formula formula-parser parse parser unity unity3d
Last synced: about 2 months ago
JSON representation
Evaluate C# Formulas at Runtime
- Host: GitHub
- URL: https://github.com/dahomey-technologies/dahomey.expressionevaluator
- Owner: dahomey-technologies
- License: other
- Created: 2017-06-10T15:40:53.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2020-10-10T11:58:56.000Z (over 4 years ago)
- Last Synced: 2024-11-10T17:47:06.389Z (3 months ago)
- Topics: csharp, dotnet, dotnet-standard, evaluate, evaluator, expression, expression-evaluator, expression-parser, formula, formula-parser, parse, parser, unity, unity3d
- Language: C#
- Size: 33.2 KB
- Stars: 41
- Watchers: 3
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: license.txt
Awesome Lists containing this project
README
# Dahomey.ExpressionEvaluator
Evaluate C# Formulas at Runtime## Supported Platforms
* .Net Standard 2.0. Compatible with
* .Net Core 2.0+
* .Net Framework 4.6.1+
* Unity 2018.1+Dahomey.ExpressionEvaluator code does not trigger any AOT complilation. It means it can be used safely with Unity IL2CPP.
## Installation
### NuGet
https://www.nuget.org/packages/Dahomey.ExpressionEvaluator/`Install-Package Dahomey.ExpressionEvaluator`
### Compilation from source
1. `dotnet restore`
2. `dotnet pack -c Release`## Examples
### Parse a numeric expression
```csharp
ExpressionParser parser = new ExpressionParser();
parser.RegisterVariable("a");
INumericExpression expr = parser.ParseNumericExpression("1 + a");int a = 2;
double result = expr.Evaluate(new Dictionary { { "a", a } });
Console.WriteLine(result);
```
The result will be:
```csharp
3
```### Parse a numeric expression with member access
```csharp
class A
{
public B B { get; set; }
}class B
{
public int Id { get; set; }
}ExpressionParser parser = new ExpressionParser();
parser.RegisterVariable("a");
INumericExpression expr = parser.ParseNumericExpression("1 + a.B.Id");A a = new A { B = new B { Id = 12 } };
double result = expr.Evaluate(new Dictionary { { "a", a } });
Console.WriteLine(result);
```
The result will be:
```csharp
13
```### Parse a numeric expression with array or list access
```csharp
ExpressionParser parser = new ExpressionParser();
parser.RegisterVariable>("a");
INumericExpression expr = parser.ParseNumericExpression("1 + a[1]");List a = new List { 1, 2 };
double result = expr.Evaluate(new Dictionary { { "a", a } });
Console.WriteLine(result);
```
The result will be:
```csharp
3
```### Parse a numeric expression with function access
```csharp
Func func = n => Math.Cos(n);
ExpressionParser parser = new ExpressionParser();
parser.RegisterFunction("cos", func);
INumericExpression expr = parser.ParseNumericExpression("1 + cos(12)");double result = expr.Evaluate()
Console.WriteLine(result);
```
The result will be:
```csharp
1.8438539587324922
```