C# .NET 4.0 表达式 eval

发布于 2024-10-02 05:52:38 字数 737 浏览 2 评论 0原文

可能的重复:
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) */

希望您明白了。

谢谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2024-10-09 05:52:38

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文