C# - 我可以根据用户提供的字符串公式动态定义函数吗?

发布于 2024-10-26 12:02:22 字数 568 浏览 0 评论 0原文

我可以使用 lambda 表达式定义一个 func,如下所示:

Func <int, int, int> Fxy = ( (a,b) => a + b ) );  // Fxy does addition

但是如果我想允许用户在运行时提供 lambda 表达式的 rhs 该怎么办?

String formula_rhs = Console.Readline();
// user enters "a - b"

我可以以某种方式修改 Fxy 吗,就像我已经编码一样,

Func <int, int, int> Fxy = ( (a,b) => a - b ) );  // Fxy does what the user wants 
                                                  // (subtraction)

我目前使用自制的表达式解析器来接受用户定义的公式。刚启动 .NET,我感觉可能有一种不太痛苦的方法可以做到这一点。

I can define a func with a lambda expression as in:

Func <int, int, int> Fxy = ( (a,b) => a + b ) );  // Fxy does addition

But what if I wanted to allow the user to supply the r.h.s. of the lambda expression at runtime ?

String formula_rhs = Console.Readline();
// user enters "a - b"

Can I somehow modify Fxy as if I had coded

Func <int, int, int> Fxy = ( (a,b) => a - b ) );  // Fxy does what the user wants 
                                                  // (subtraction)

I presently use a home built expression parser to accept user-defined formulas. Just starting up w .NET, and I get the feeling there may be a not-too-painful way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

断肠人 2024-11-02 12:02:22

尝试 ncalc 框架:http://ncalc.codeplex.com/
它简单且轻便。

Try the ncalc framework: http://ncalc.codeplex.com/
Its easy and lightweight.

陈甜 2024-11-02 12:02:22

没那么容易。 此库包含一些可用于转换 lambda 表达式的 Dynamic Linq 功能从text到Func<>,看一下。

Is not so easy. This library contains some Dynamic Linq features you can use to convert a lambda expression from text to Func<>, have a look at it.

2024-11-02 12:02:22

System.Reflection.Emit 命名空间用于动态代码生成。一般来说,它相当复杂,但可能对减法等简单函数有帮助。

System.Reflection.Emit namespace is intended for code generation on-the-fly. It is quite complicated in general but may help for simple functions like subtraction.

泪是无色的血 2024-11-02 12:02:22

如果您计划使用不复杂的公式,JScript 引擎将提供简单的解决方案:

        Microsoft.JScript.Vsa.VsaEngine engine = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
        MessageBox.Show(Microsoft.JScript.Eval.JScriptEvaluate("((2+3)*5)/2", engine).ToString());

不要忘记添加对 Microsoft.JScript 程序集的引用

If you are planning to use not complex formulas, JScript engine would easy solution:

        Microsoft.JScript.Vsa.VsaEngine engine = Microsoft.JScript.Vsa.VsaEngine.CreateEngine();
        MessageBox.Show(Microsoft.JScript.Eval.JScriptEvaluate("((2+3)*5)/2", engine).ToString());

Dont forget to add reference to Microsoft.JScript assembly

怕倦 2024-11-02 12:02:22

使用 Linq.Expressions 可能是一个想法。

您的示例将转换为:

var a = Expression.Parameter(typeof(int), "a");
var b = Expression.Parameter(typeof(int), "b");
var sum = Expression.Subtract(a, b);
var lamb = Expression.Lambda<Func<int, int, int>>(sum, a, b);
Func<int, int, int> Fxy = lamb.Compile();

当然,您将享受将字符串解析为正确表达式的乐趣。 :-)

Using Linq.Expressions could be an idea here.

Your example would tranlate to:

var a = Expression.Parameter(typeof(int), "a");
var b = Expression.Parameter(typeof(int), "b");
var sum = Expression.Subtract(a, b);
var lamb = Expression.Lambda<Func<int, int, int>>(sum, a, b);
Func<int, int, int> Fxy = lamb.Compile();

Ofcourse, you will have the fun of parsing a string into the correct expressions. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文