需要 .Net 的公式解释器
我正在寻找一个可以在 C# 应用程序中使用的公式解释器。它需要能够解释这样的字符串:
max(1+2, 4) * x
我发现编写一个快速公式解释器(codeproject .com) 几乎可以满足我的需要,但它不允许具有多个参数的函数。我可能可以添加该功能,但我只是想知道这样的功能是否已经存在。
谢谢
I'm looking for a formula interpreter that I can use in a C# application. It needs to be able to interpret a string like this:
max(1+2, 4) * x
I found Writing a fast formula interpreter (codeproject.com) which almost does what I need but it doesn't allow for functions with multiple parameters. I could probably add that functionality to it but I was just wondering if something like this already exists.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我过去使用过的几个没有问题:
A couple I've used in the past with no problems:
实际上,您可以通过解析和替换某些功能关键字(例如
max
)来构建一个非常有效的解释器 Math.Max,然后动态将公式构建为 C# 类和方法并执行。因此,实际上您将解析和包装公式并允许 C# 编译器解释和执行它。因此,采用
max(1+2, 4) * x
的示例公式将变成:您将即时编译该公式,然后根据链接的文章执行。当然,您仍然需要解析并传递 x 值。
You can actually build a very effective interpreter by parsing and replacing certain functional keywords such as
max
with Math.Max and then dynamically building and executing the formula as a C# class and method. So actually you would be parsing and wrapping the formula and allowing the C# compiler to interpret and execute it.So, taking your sample formula of
max(1+2, 4) * x
would turn into:Which you would compile on the fly and then execute per the linked article. You still have to parse for and pass the x value of course.
很久以前,在一个项目中,我必须使用公式创建一些预订,并且我使用了 VsaEngine。要使用此引擎,您需要添加对 Microsoft.JScript 的引用。这是示例:
使用代码非常简单,只需替换公式参数即可,例如:
这里是核心方法,CalculateFormula:
使用此方法,您可以创建自定义公式解释器引擎。
A long time ago in one project i had to create some booking with formulas, and i used VsaEngine. To use this engine you need to add reference to
Microsoft.JScript
. Here is example:Code for usage is very simple, just replace formula parameters like:
And here is core method, CalculateFormula:
With this you can create your custom formula interpreter engine.