我想将字符串转换为条件运算符,一些示例:
类似这样的事情:
public static class Extension
{
public static Operator ConditionalOperator(this string logic)
{
switch (logic)
{
case "and": return // return && conditionaloperator
case "or": return // return || conditionaloperator
case "greatherthan": return // return >= conditionaloperator
}
}
}
第二个参数并不总是字符串,但可以是 int、double、bool、...
我想构建类似的东西:
ConditionalOperator("==", "StringToSearch")
返回一个Func<>
你知道如何做到这一点吗?
谢谢,
public static class Extension
{
public static Boolean Operator(this string logic, int x, int y)
{
switch (logic)
{
case ">": return x > y;
case "<": return x < y;
case "==": return x == y;
default: throw new Exception("invalid logic");
}
}
}