Himmelt.NFun
1.2.2
dotnet add package Himmelt.NFun --version 1.2.2
NuGet\Install-Package Himmelt.NFun -Version 1.2.2
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="Himmelt.NFun" Version="1.2.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Himmelt.NFun" Version="1.2.2" />
<PackageReference Include="Himmelt.NFun" />
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 Himmelt.NFun --version 1.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Himmelt.NFun, 1.2.2"
#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.
#:package Himmelt.NFun@1.2.2
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Himmelt.NFun&version=1.2.2
#tool nuget:?package=Himmelt.NFun&version=1.2.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
<div align="center">
NFun .NET 表达式求值器
</div>
关于
本项目Fork自 tmteam/NFun 并对其进行了一些修改。
主要添加功能为,扩展WithFunction方法,支持Delegate委托类型的函数。
安装
要安装 NFun,请在包管理器控制台中运行以下命令:
PM> Install-Package Himmelt.NFun
什么是 NFun?
这是一个表达式求值器或 .NET 的迷你脚本语言。它支持处理数学表达式以及集合、字符串、高阶函数和结构。NFun 与 NCalc 非常相似,但具有丰富的类型系统和 LINQ 支持。 详细信息请参阅 '使用指南/规范' 部分
NFun 可以执行简单的求值
double d = Funny.Calc<double>(" 2 * 10 + 1 ") // 21
bool b = Funny.Calc<bool>("false and (2 > 1)") // false
// 'age' 和 'name' 来自输入 'User' 模型的属性
string userAlias = Funny.Calc<User,string> (
"if(age < 18) name else 'Mr. {name}' ",
inputUser)
也可以执行复杂的、多复合输入和输出的操作
// 求值多个值并将它们设置到 'Person' 对象的属性中
// 输入和输出 'age', 'cars' 和 'birthYear' 都是 'Person' 对象的属性
var personModel = new Person(birthYear: 2000);
Funny.CalcContext(@"
age = 2022 - birthYear
cars = [
{ name = 'lada', cost = 1200, power = 102 },
{ name = 'camaro', cost = 5000, power = 275 }
]
", personModel);
Assert.Equal(22, personModel.Age);
Assert.Equal(2, personModel.Cars.Count());
Assert.Equal(1200, personModel.Cars[0].Cost);
也支持底层硬核 API
var runtime = Funny.Hardcore.Build("y = 2x+1");
runtime["x"].Value = 42; //写入输入数据
runtime.Run(); //运行脚本
var result = runtime["y"].Value //收集结果
Console.WriteLine("脚本包含这些变量:"
foreach(var variable in runtime.Variables)
Console.WriteLine(
"{variable.Name}:{variable.Type} {variable.IsOutput?"[OUTPUT]":"[INPUT]"}");
主要功能
- 算术、按位、逻辑和比较运算符
# 算术运算符: + - * / % // **
y1 = 2*(x//2 + 1) / (x % 3 -1)**0.5 + 3x
# 按位: ~ | & ^ << >>
y2 = (x | y & 0xF0FF << 2) ^ 0x1234
# 逻辑和比较: and or not > >= < <= == !=
y3 = x and false or not (y>0)
- If 表达式
simple = if (x>0) x else if (x==0) 0 else -1
complex = if (age>18)
if (weight>100) 1
if (weight>50) 2
else 3
if (age>16) 0
else -1
- 用户函数和泛型算术
sum3(a,b,c) = a+b+c
r:real = sum3(1,2,3)
i:int = sum3(1,2,3)
- 数组、字符串、数字、结构和高阶函数支持
out = {
name = 'etaK'.reverse()
values = [0xFF0F, 2_000_000, 0b100101]
items = [1,2,3].map(rule 'item {it}')
}
- 严格类型系统和类型推导算法
y = 2x
z:int = y*x
m:real[] = [1,2,3].map(rule it/2)
- 双精度或十进制算术
- 语法和语义定制
- 内置函数
- 注释
- 可选类型、类型收窄、命名类型等
如何使用
详细的规范比没有规范好
| 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 is compatible. 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 is compatible. 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.
-
net10.0
- IndexRange (>= 1.1.1)
-
net8.0
- IndexRange (>= 1.1.1)
-
net9.0
- IndexRange (>= 1.1.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.2.2 | 77 | 8/6/2026 |
| 1.0.3-beta1 | 235 | 12/7/2025 |
扩展 WithFunction Delegate 方法;同步上游 NFun v1.2.2