简化到只是比较布尔值,但如果保留了,否则......
CodeEntryPointMethod start = new CodeEntryPointMethod();
//...
start.Statements.Add(new CodeVariableDeclarationStatement(typeof(bool), "ifCheck", new CodePrimitiveExpression(false)));
var e1 = new CodeBinaryOperatorExpression(new CodePrimitiveExpression(false), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(false));
var e2 = new CodeBinaryOperatorExpression(new CodePrimitiveExpression(false), CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(true));
var ifAssign = new CodeAssignStatement(new CodeVariableReferenceExpression("ifCheck"), new CodeBinaryOperatorExpression(e1, CodeBinaryOperatorType.BooleanOr, e2));
start.Statements.Add(ifAssign);
var x1 = new CodeVariableDeclarationStatement(typeof(string), "x1", new CodePrimitiveExpression("Anything here..."));
var ifCheck = new CodeConditionStatement(new CodeVariableReferenceExpression("ifCheck"), new CodeStatement[] { x1 }, new CodeStatement[] { x1 });
start.Statements.Add(ifCheck);
产生:
bool ifCheck = false;
ifCheck = ((false == false)
|| (false == true));
if (ifCheck) {
string x1 = "Anything here...";
}
else {
string x1 = "Anything here...";
}
来源
2015-06-02 16:55:21
tic
内部支架是在贴例子没用,因为结果是相同或不相同(即使它评估的顺序应该是相同的)! – ChrFin
...虽然它确实帮助可视化表示条件的表达式树 –
将代码示例的布尔逻辑更改为需要括号 – Jon