Additional brackets in CodeDom code.

I use CodeDom to generate code that will be compiled later, and I noticed that some constructs create additional sets of brackets. Although I know that they do not affect anything, they look strange.

Sample code that does this:

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

This generates the following snippet:

if ((value.Length > 50)) {
    throw new System.ArgumentException("The string is too long", "value");
}

Again, I know that extra parentheses do not affect anything, but if I do something wrong to do this, I would like to know :)

+3
source share
2 answers

, CodeDom , , . .

+1

. .

gen #, CodeSnippetExpression, . .

IL ( ), , , .

+1

All Articles