我使用CodeDom生成代码以后编译,我注意到某些构造会创建额外的括号。虽然我知道它们不会影响任何东西,但看起来很奇怪。CodeDom生成的代码中的额外括号
的代码样本,做它是这样的:
new CodeConditionStatement(
new CodeBinaryOperatorExpression(
new CodePropertyReferenceExpression(new CodePropertySetValueReferenceExpression(),
"Length"),
CodeBinaryOperatorType.GreaterThan,
new CodePrimitiveExpression(strLength)
),
new CodeThrowExceptionStatement(
new CodeObjectCreateExpression(typeof(ArgumentException),
new CodePrimitiveExpression("The string is too long"),
new CodePrimitiveExpression("value"))
)
)
这会产生下面的代码片段:
if ((value.Length > 50)) {
throw new System.ArgumentException("The string is too long", "value");
}
再次,我知道额外的括号不会影响任何东西,但如果我做错了这样做,我想知道:)
这似乎与任何事情一样合理。毕竟,生成的代码并不意味着被查看:) – 2011-05-30 20:57:46
@Mike Caron:除了当您试图调试生成的代码或代码生成器本身时。去过也做过。 (并不是说这是一个额外的大括号问题,但:-) – 2011-05-31 04:25:00