如何在C#中使用字符串作为if条件语句的一部分?

7

这些值来自于 xml 文件,用户只需要声明要执行的条件。

string condition ="25<10";

现在,我想在if条件语句中使用它,例如:
if(condition)
{
    //my condition
}

我遇到了这个错误

Cannot implicitly convert type string to bool

有人能告诉我如何做到这一点吗?


1
https://ncalc.codeplex.com/ - L.B
当然你不能直接将字符串转换为布尔值(if条件需要布尔值)。你需要使用数值比较运算符来完成这个过程(if (25 < 10)是有效的,但if ("25 < 10")是无效的)。 - Tetsuya Yamamoto
3
为什么用户没有指定“false”或“no”呢?我怀疑您实际的表达更加复杂,有效答案取决于其复杂程度。可以参考@L.B的评论,但根据实际语法,这可能不足或甚至不正确。直白地说,如果在确定如何处理已开始的表达式之前,您决定了语法,那么您可能会发现自己允许了必须自行实现支持的语法。 - Lasse V. Karlsen
Expression Builder的最佳示例之一是Kendar Expression builder,您可以创建具有布尔返回类型的特定操作条件。 - Mohit S
可能是在C#中以字符串格式检查条件的重复问题。 - ASh
显示剩余3条评论
3个回答

8

它正在运行,谢谢你的建议。 - Vinoth
1
嗨,Dmitry Bychenko,如果条件是1!= 10,则此条件无效,我该怎么办? - Vinoth
请使用<>代替!= - Dmitry Bychenko

0
首先: 如果你这么做:string condition ="25<10"condition的值将会是25<10而不是true或false!如果25,10和<号来自于你的xml,请把它们分别放入三个不同的字符串比如x、y和z,并像下面这样进行比较:

string input = "25<10"; //input form your xml
int operatorPosition;
//get the position of the operator
if(input.contains("<")){
  operatorPosition = input.IndexOf("<");
}else{
  operatorPosition = input.IndexOf(">");
}
//maybe i messed up some -1 or +1 here but this should work this
string x = input.Substring(0,operatorPosition-1);
string y = input.Substring(operatorPosition+1, input.length-1);
string z = input.charAt(operatorPosition);

//check if it is < or > 
if(z.equals("<"){
  //compare x and y with <
  if(Int32.parse(x) < Int32.parse(y)){
    //do something
  }else{
    //do something
  }
}
//z does not equal < so it have to be > (if you dont have something like = otherwise you need to check this too)
else{
  if(Int32.parse(x) < Int32.parse(y)){
    //do something
  }else{
    //do something
  }

也许有更好的方法将纯字符串输入转换为 if 语句,但这是我会选择的方式。

假设 OP 在单个字符串中组合了表达式,如 x = "25 < 10"。这需要使用 Contains 方法来确定小于运算符和 String.Split 来获取两个操作数,然后可以在 if 条件语句中进行比较。 - Tetsuya Yamamoto
是的,你说得对。我会在几秒钟内进行更正。 - T. Jung
虽然答案可能回答了问题,但它相当特定于该问题,因此除了提问者之外,在StackOverflow上可能没有太大的用处。 - MakePeaceGreatAgain
嗨,T.Jung,有没有不使用if条件的方法,因为如果我们在IF中没有提到任何条件,那么条件就不起作用,所以我想问一下。 - Vinoth
@Vinoth - “如果我们在IF语句中没有提到任何条件,那么条件就不会起作用,这是我的疑问”的意思是什么? - Enigmativity

0
你可以使用以下代码来实现它:
string statment = "10<25"; // Sample statement
string leftOperand = statment.Split('<').First();
string rightOperand = statment.Split('<').Last();
int relationValue = Math.Sign(leftOperand.CompareTo(rightOperand));
if(relationValue == -1)
{
    // leftOperand < rightOperand
}
else if (relationValue == 0)
{
    // leftOperand == rightOperand
}
else if (relationValue == 1)
{
    // leftOperand > rightOperand
}

如果您只想检查leftOperand < rightOperand,您可以使用三元运算符,如下所示:

bool condition = Math.Sign(leftOperand.CompareTo(rightOperand)) == -1 ? true : false;

嗨,阿里,有没有不使用if条件的方法,因为如果我们没有在IF中编写所有条件,那么用户将无法获得结果,所以我想问一下。 - Vinoth
你可以使用类似于这样的代码 condition ? first_expression : second_expression;,但它仍然是一个 if 条件语句的简短版本。 - T. Jung
@Vinoth 你只是想检查它是否更大(leftOperand < rightOperand)吗? - Ali Soltani
@Vinoth,我添加了一段代码来检查leftOperand < rightOperand,而不需要使用if。请看这个。 - Ali Soltani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接