Codedom 生成复杂的 if 语句

发布于 2024-12-02 05:33:49 字数 364 浏览 1 评论 0原文

我有点坚持尝试生成如下所示的复杂 if 语句

if (class1.Property == class2.Property || (class3.Property && class4.Property))
{
  //do something
}
else
{
   //do something else
}

通过使用 CodeConditionStatement 类,我可以生成上面的第一个条件,但我似乎找不到添加第二个条件的方法,特别是使用所需的括号和if 将在运行时评估的方式?

注意:我不想使用 CodeSnippetExpression 类。

有什么想法吗?

提前致谢。

I'm a bit stuck with try to generate a complex if statement like the one below

if (class1.Property == class2.Property || (class3.Property && class4.Property))
{
  //do something
}
else
{
   //do something else
}

By using the CodeConditionStatement class I can generate the first condition above but I can not seem to find a way to add the second condition especially with the needed brackets and the way if will be evaluated at runtime?

Note: I do not want to use the CodeSnippetExpression class.

Any ideas?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

温柔一刀 2024-12-09 05:33:49

将条件分成 3 个二进制表达式: (class1.Property == class2.Property || (class3.Property || class4.Property)

  1. class3.Property || class4.Property - CodeBinaryOperatorExpression 与 CodePropertyReferenceExpression 分别位于左侧和右侧
  2. class1.Property == class2.Property - CodeBinaryOperatorExpression 与 CodePropertyReferenceExpression 位于左侧和右侧
  3. 最后 #2 || #1 - CodeBinaryOperatorExpression #2 位于左侧,#1 位于右侧

Separate the condition into 3 binary expressions: (class1.Property == class2.Property || (class3.Property || class4.Property)

  1. class3.Property || class4.Property - CodeBinaryOperatorExpression with CodePropertyReferenceExpression on left and right
  2. class1.Property == class2.Property - CodeBinaryOperatorExpression with CodePropertyReferenceExpression on left and right
  3. Finally #2 || #1 - CodeBinaryOperatorExpression #2 on left and #1 on right
心如荒岛 2024-12-09 05:33:49

首先,一个简单的方法是声明一个布尔变量。
将其初始化为 false 并初始化为 if 操作该变量的序列。
不要担心性能或可读性,有时它更具可读性。

bool x = false;
if (class1.Property == class2.Property)
{
    x = true;
}
if (class3.Property == class4.Property)
{
    x = true;
}

if (!anotherthingtocheck)
    x = false;

if (x)
{
    // Do something
}
else
{
    // Do something else
}

First, a simple way would be to declare a boolean variable.
Initialize it to false and to the sequence of if manipulating that variable.
Don't worry about performance or readibility, sometime it is more readable.

bool x = false;
if (class1.Property == class2.Property)
{
    x = true;
}
if (class3.Property == class4.Property)
{
    x = true;
}

if (!anotherthingtocheck)
    x = false;

if (x)
{
    // Do something
}
else
{
    // Do something else
}
煮酒 2024-12-09 05:33:49

简化为仅比较布尔值,但保留 if, else...

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...";
}

Simplified to just compare booleans, but retaining the if, else...

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);

generates:

bool ifCheck = false;
ifCheck = ((false == false) 
    || (false == true));
if (ifCheck) {
    string x1 = "Anything here...";
}
else {
    string x1 = "Anything here...";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文