C# .NET 4.0表达式求值

3

可能是重复问题:
C# eval等效功能?
如何动态评估C#表达式?

如何在编译的程序中评估有效的C#代码并获取结果(动态)? 我知道这不是很清楚,我会尝试用代码片段来展示:

Visual C# 2010。

double x = 3.0;//this is function argument, we'll take 3
string str = "Math.Pow(x,2)+2*x+4";//value with written function in C#,equals: x^2+2x+4
double y = eval(str);
/* this is the idea! something like JS.I want to make string parsed into parameter,i.e
y=19 - this is what double y should became after eval(str) */

希望您已经理解了这个概念。 谢谢。

请仅返回翻译后的文本:以及 https://dev59.com/R3NA5IYBdhLWcg3wSrua - Robert Harvey
1个回答

1

不,LINQ 不会为您完成此操作。

目前您可以轻松获得的最接近的方法是使用 CSharpCodeProvider,它允许您编译整个程序集 - 但您需要将表达式包装在所有其他源代码中,以使其成为完整的类。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接