Is there a string math evaluator in .NET?(.NET 中是否有字符串数学评估器?)
问题描述
如果我有一个包含有效数学表达式的字符串,例如:
If I have a string with a valid math expression such as:
String s = "1 + 2 * 7";
.NET 中是否有内置库/函数可以为我解析和评估该表达式并返回结果?在这种情况下 15.
Is there a built in library/function in .NET that will parse and evaluate that expression for me and return the result? In this case 15.
推荐答案
您可以添加对 Microsoft 脚本控件库 (COM) 的引用,并使用这样的代码来评估表达式.(也适用于 JScript.)
You could add a reference to Microsoft Script Control Library (COM) and use code like this to evaluate an expression. (Also works for JScript.)
Dim sc As New MSScriptControl.ScriptControl()
sc.Language = "VBScript"
Dim expression As String = "1 + 2 * 7"
Dim result As Double = sc.Eval(expression)
编辑 - C# 版本.
MSScriptControl.ScriptControl sc = new MSScriptControl.ScriptControl();
sc.Language = "VBScript";
string expression = "1 + 2 * 7";
object result = sc.Eval(expression);
MessageBox.Show(result.ToString());
编辑 - ScriptControl 是一个 COM 对象.在项目的添加引用"对话框中选择COM"选项卡并向下滚动到Microsoft Script Control 1.0"并选择确定".
Edit - The ScriptControl is a COM object. In the "Add reference" dialog of the project select the "COM" tab and scroll down to "Microsoft Script Control 1.0" and select ok.
这篇关于.NET 中是否有字符串数学评估器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:.NET 中是否有字符串数学评估器?


基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01