Codedom 生成复杂的 if 语句
我有点坚持尝试生成如下所示的复杂 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将条件分成 3 个二进制表达式:
(class1.Property == class2.Property || (class3.Property || class4.Property)
class3.Property || class4.Property
- CodeBinaryOperatorExpression 与 CodePropertyReferenceExpression 分别位于左侧和右侧class1.Property == class2.Property
- CodeBinaryOperatorExpression 与 CodePropertyReferenceExpression 位于左侧和右侧#2 || #1
- CodeBinaryOperatorExpression #2 位于左侧,#1 位于右侧Separate the condition into 3 binary expressions:
(class1.Property == class2.Property || (class3.Property || class4.Property)
class3.Property || class4.Property
- CodeBinaryOperatorExpression with CodePropertyReferenceExpression on left and rightclass1.Property == class2.Property
- CodeBinaryOperatorExpression with CodePropertyReferenceExpression on left and right#2 || #1
- CodeBinaryOperatorExpression #2 on left and #1 on right首先,一个简单的方法是声明一个布尔变量。
将其初始化为 false 并初始化为 if 操作该变量的序列。
不要担心性能或可读性,有时它更具可读性。
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.
简化为仅比较布尔值,但保留 if, else...
生成:
Simplified to just compare booleans, but retaining the if, else...
generates: