提交 721c56fc 编写于 作者: S Sergio Pedri

Fix forwarded attributes with negative values

上级 e071ed22
...@@ -135,8 +135,17 @@ public override ExpressionSyntax GetSyntax() ...@@ -135,8 +135,17 @@ public override ExpressionSyntax GetSyntax()
public override ExpressionSyntax GetSyntax() public override ExpressionSyntax GetSyntax()
{ {
// We let Roslyn parse the value expression, so that it can automatically handle both positive and negative values. This // We let Roslyn parse the value expression, so that it can automatically handle both positive and negative values. This
// is needed because negative values have a different syntax tree (UnaryMinuxExpression holding the numeric expression). // is needed because negative values have a different syntax tree (UnaryMinusExpression holding the numeric expression).
return CastExpression(IdentifierName(TypeName), ParseExpression(Value.ToString())); ExpressionSyntax valueExpression = ParseExpression(Value.ToString());
// If the value is negative, we have to put parentheses around them (to avoid CS0075 errors)
if (valueExpression is PrefixUnaryExpressionSyntax unaryExpression && unaryExpression.IsKind(SyntaxKind.UnaryMinusExpression))
{
valueExpression = ParenthesizedExpression(valueExpression);
}
// Now we can safely return the cast expression for the target enum type (with optional parentheses if needed)
return CastExpression(IdentifierName(TypeName), valueExpression);
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册