注意:数学表达式评估不是这个问题的焦点.我想在.NET中运行时编译和执行新代码. 话虽如此...
我想允许用户在文本框中输入任何等式,如下所示:
x = x / 2 * 0.07914
x = x^2 / 5
Run Code Online (Sandbox Code Playgroud)
并将该等式应用于传入的数据点.输入数据点由x表示,每个数据点由用户指定的等式处理.我多年前做过,但我不喜欢这个解决方案,因为它需要为每次计算解析等式的文本:
float ApplyEquation (string equation, float dataPoint)
{
// parse the equation string and figure out how to do the math
// lots of messy code here...
}
Run Code Online (Sandbox Code Playgroud)
当您处理大量数据点时,这会引入相当多的开销.我希望能够在飞行中将方程转换为函数,这样它只需要解析一次.它看起来像这样:
FunctionPointer foo = ConvertEquationToCode(equation);
....
x = foo(x); // I could then apply the equation to my incoming data like this
Run Code Online (Sandbox Code Playgroud)
函数ConvertEquationToCode将解析方程并返回指向应用适当数学的函数的指针.
该应用程序基本上是在运行时编写新代码.这可能与.NET有关吗?