C# .NET 4.0 表达式 eval
如何评估有效的 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) */
希望您明白了。
谢谢。
Possible Duplicates:
C# eval equivalent?
How can I evaluate a C# expression dynamically?
How can I evaluate a valid C# code and get results inside a compiled program (dynamically)?
I know this is not very clear, i'll try to show that with a code snippet:
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) */
Hope you got the idea.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,LINQ 不会为您执行此操作。
目前您可以轻松获得的最接近的是使用 CSharpCodeProvider 它允许您编译整个程序集 - 但您需要将表达式包装在使其成为完整类所需的所有其他源代码中。
No, LINQ won't do this for you.
The closest you can easily get at the moment is to use CSharpCodeProvider which allows you to compile a whole assembly - but you'll need to wrap your expression in all the other source code required to make it a complete class.