我正在从一些业务规则生成表达式,它可能看起来像这样
0 > 1
12 < 14
"abc" != "xyz"
90 >= 12
现在我必须根据这个条件做某些实现。例如:
string condition = "0 =1";
if(condition)
{
// do something because condition is passed
}
else
{
// do something because condition is failed
}
我尝试使用动态关键字做同样的事情,但它仍无效。 有什么工作吗?
修改:1 修改后的代码
string _initExp = "1";
string _validateCondition = "== 0";
string strcondition = _initExp + _validateCondition;
bool _condition = Convert.ToBoolean(strcondition); // Error statement
if (_condition)
{
}
答案 0 :(得分:4)
为什么不使用bool
:
bool condition = 0==1;
if(condition)
{
// do something because condition is passed
}
else
{
// do something because condition is failed
}
您还可以简化以下代码:
bool condition = 0==1;
if(condition)
{
return true;
}
else
{
return false;
}
为:
bool condition = 0==1;
return condition;
或自定义返回值
bool condition = 0==1;
return condition ? "yes" : "no";