File tree Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Expand file tree Collapse file tree 1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -135,8 +135,17 @@ public sealed record Enum(string TypeName, object Value) : TypedConstantInfo
135
135
public override ExpressionSyntax GetSyntax ( )
136
136
{
137
137
// We let Roslyn parse the value expression, so that it can automatically handle both positive and negative values. This
138
- // is needed because negative values have a different syntax tree (UnaryMinuxExpression holding the numeric expression).
139
- return CastExpression ( IdentifierName ( TypeName ) , ParseExpression ( Value . ToString ( ) ) ) ;
138
+ // is needed because negative values have a different syntax tree (UnaryMinusExpression holding the numeric expression).
139
+ ExpressionSyntax valueExpression = ParseExpression ( Value . ToString ( ) ) ;
140
+
141
+ // If the value is negative, we have to put parentheses around them (to avoid CS0075 errors)
142
+ if ( valueExpression is PrefixUnaryExpressionSyntax unaryExpression && unaryExpression . IsKind ( SyntaxKind . UnaryMinusExpression ) )
143
+ {
144
+ valueExpression = ParenthesizedExpression ( valueExpression ) ;
145
+ }
146
+
147
+ // Now we can safely return the cast expression for the target enum type (with optional parentheses if needed)
148
+ return CastExpression ( IdentifierName ( TypeName ) , valueExpression ) ;
140
149
}
141
150
}
142
151
You can’t perform that action at this time.
0 commit comments