Skip to main content
 首页 » 编程设计

c#之来自字符串的动态 if 条件

2024年02月13日21lvdongjie

<分区>

我正在根据一些业务规则生成一个表达式,它可能看起来像这样

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) 
{ 
 
}