动态if语句评估/生成 [英] Dynamic if statement evaluation/generation

查看:66
本文介绍了动态if语句评估/生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想评估动态if语句,例如下面的

Hello everyone,

I want to evaluate dynamic if statement like the e.g below

int a=2;
String condition="a==2";

if(condition)
execute something
else
execute something


我的意思是条件是可变的,它来自数据库.
例如:


I mean that the condition is changeable, and it''s coming from database.
For example:

String condition="";
int a, b, c, d;


....
条件可能是:


....
The condition could be:

condition = "a == 2";


或可能是:


or could be:

condition = "a >= b && c == a && ((d - 4) == a ) ";




or

condition = ".....something else......";





if(condition)
execute something
else
execute something



条件不是恒定的.那是我的问题.

我该怎么做...请让我知道.
在此先感谢

[修改:只是将代码标记更改为前置标记...以这种方式更易于阅读IMO]



The condition is not constant. That''s my problem.

How i can do this... Please let me know..
Thanks in advance

[Modified: just changed the code tags to pre tags...a bit easier to read that way IMO]

推荐答案

使用内置的.NET框架可能会在代码编译器中.我从来没有自己做过,但是我已经看过用IronRuby等做过的演示.诚然,它们是动态语言,但是应该可以用C#来做.

[此处 [
It might be possible using the .NET framework built in code compiler. I have never done this myself, but i have seen demos of it done with IronRuby, etc. Admittedly they were dynamic languages, but should be possible to do it in C#.

This[^] article on CodeProject might help.

There is also a calculator example here[^].

If the user is able to enter the a ==2 etc, you will have to be very careful checking input to make sure they don''t do anything nasty. Not sure how you would go about doing this.


您需要编写一个表达式评估器,该评估器可以接收字符串,解析字符串并使用变量评估数学表达式.没有内置功能可以执行此操作,因此您需要编写自己的代码或搜索第三方实现.
You need to write an expression evaluator that can take a string, parse it, and evaluate a mathematical expression with variables. There''s nothing built-in that can do this, so you need to write your own or search for a 3rd party implementation.


对于您的问题,我不太清楚.

以下代码(C#)有什么问题:
I am not very clear about your question.

What''s the problem with the below code (C#):
int a = 2;
if (a == 2)
{
    //execute something
}
else
{
    //execute something else
}


那么,通过评估动态if语句意味着什么?


And what do you mean by evaluating dynamic if statement?


这篇关于动态if语句评估/生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆