Com.RickSeven.FormulaEngine.Core
1.0.4
dotnet add package Com.RickSeven.FormulaEngine.Core --version 1.0.4
NuGet\Install-Package Com.RickSeven.FormulaEngine.Core -Version 1.0.4
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Com.RickSeven.FormulaEngine.Core" Version="1.0.4" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Com.RickSeven.FormulaEngine.Core" Version="1.0.4" />
<PackageReference Include="Com.RickSeven.FormulaEngine.Core" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Com.RickSeven.FormulaEngine.Core --version 1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Com.RickSeven.FormulaEngine.Core, 1.0.4"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=Com.RickSeven.FormulaEngine.Core&version=1.0.4
#tool nuget:?package=Com.RickSeven.FormulaEngine.Core&version=1.0.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Com.RickSeven.FormulaEngine
A powerful and flexible formula evaluation engine using Roslyn Scripting with dynamic runtime context. Ideal for scenarios where business logic needs to be defined at runtime through C# expressions or formulas.
🚀 Features
- Evaluate C# expressions dynamically at runtime
- Support for nested objects and complex data structures
- JSON context support for easy integration
- Rich set of operations:
- Numeric operations (arithmetic, Math functions)
- String manipulation and formatting
- DateTime operations
- Logical operations and conditionals
- Collection handling with indexing
- Culture-specific formatting support
- Comprehensive error handling
- No external dependencies other than Roslyn and BCL
🎞 Installation
Install via NuGet:
dotnet add package Com.RickSeven.FormulaEngine.Core --version 1.0.0
Or using the NuGet Package Manager:
PM> Install-Package Com.RickSeven.FormulaEngine.Core
🧠 Usage Examples
Basic Operations
var context = new Dictionary<string, object>
{
{ "revenue", 10000 },
{ "cost", 7000 },
{ "margin", 0.3 },
{ "desc", "Result is: " }
};
string formula = "desc + ((revenue - cost) * margin).ToString()";
var engine = new FormulaEngine();
var result = engine.Evaluate(formula, context);
// Output: "Result is: 900"
Console.WriteLine(result);
Math Functions
var context = new Dictionary<string, object>
{
{ "baseValue", 2.0 },
{ "exponent", 3.0 }
};
string formula = "Math.Pow(baseValue, exponent)";
var engine = new FormulaEngine();
var result = engine.Evaluate(formula, context);
// Output: 8.0
Console.WriteLine(result);
Culture-Specific Formatting
var jsonContext = @"{
""revenue"": 1000.50,
""cost"": 750.25,
""taxRate"": 0.2,
""description"": ""Profit calculation: ""
}";
var formula = @"description + ((revenue - cost) * (1 - taxRate)).ToString(""C2"", new System.Globalization.CultureInfo(""id-ID""))";
var engine = new FormulaEngine();
var result = engine.Evaluate(formula, jsonContext);
// Output: Profit calculation: Rp200,20
Console.WriteLine(result);
Nested Objects and Collections
var jsonContext = @"{
""person"": {
""firstName"": ""John"",
""lastName"": ""Doe"",
""phones"": [
{""type"": ""home"", ""number"": ""555-1234""},
{""type"": ""work"", ""number"": ""555-5678""}
]
},
""company"": {
""name"": ""Acme Corp"",
""address"": {
""city"": ""Silicon Valley"",
""coordinate"": {
""latitude"": 37.37,
""longitude"": -122.04
}
}
}
}";
var engine = new FormulaEngine();
// Access nested properties
var result1 = engine.Evaluate("person.firstName + \" \" + person.lastName", jsonContext);
// Output: "John Doe"
// Access array elements
var result2 = engine.Evaluate("person.phones[0].number", jsonContext);
// Output: "555-1234"
// Complex nested property access with string interpolation
var result3 = engine.Evaluate(@"$""{person.firstName} works in {company.address.city}""", jsonContext);
// Output: "John works in Silicon Valley"
// Conditional array access
var result4 = engine.Evaluate("person.phones[person.phones[0].type == \"home\" ? 0 : 1].number", jsonContext);
// Output: "555-1234"
DateTime Operations
var context = new Dictionary<string, object>
{
{ "date", new DateTime(2023, 1, 1) }
};
var formula = "date.AddDays(10).Day";
var engine = new FormulaEngine();
var result = engine.Evaluate(formula, context);
// Output: 11
Console.WriteLine(result);
Error Handling
try
{
var context = new Dictionary<string, object> { { "x", 5 } };
var engine = new FormulaEngine();
var result = engine.Evaluate("x + y", context); // 'y' is not defined
}
catch (FormulaEvaluationException ex)
{
// Handle evaluation error
Console.WriteLine(ex.Message);
}
📝 Notes
- The engine supports full C# expression syntax
- Nested objects can be accessed using dot notation
- Array elements can be accessed using index notation
- JSON context is automatically parsed and made available to formulas
- Culture-specific formatting is supported through standard .NET formatting
- Error handling is provided through FormulaEvaluationException
- Variable names cannot use C# reserved keywords (e.g., 'base', 'class', 'static', etc.)
📧 Support
For bug reports or support inquiries, please contact:
- Email: maseric7@gmail.com
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.13.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.