本文整理汇总了C#中ExpressionSyntax.Parenthesize方法的典型用法代码示例。如果您正苦于以下问题:C# ExpressionSyntax.Parenthesize方法的具体用法?C# ExpressionSyntax.Parenthesize怎么用?C# ExpressionSyntax.Parenthesize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExpressionSyntax
的用法示例。
在下文中一共展示了ExpressionSyntax.Parenthesize方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Negate
private ExpressionSyntax Negate(ExpressionSyntax expression, SemanticModel semanticModel, CancellationToken cancellationToken)
{
ExpressionSyntax result;
if (TryNegateBinaryComparisonExpression(expression, semanticModel, cancellationToken, out result))
{
return result;
}
switch (expression.Kind())
{
case SyntaxKind.ParenthesizedExpression:
{
var parenthesizedExpression = (ParenthesizedExpressionSyntax)expression;
return parenthesizedExpression
.WithExpression(Negate(parenthesizedExpression.Expression, semanticModel, cancellationToken))
.WithAdditionalAnnotations(Simplifier.Annotation);
}
case SyntaxKind.LogicalNotExpression:
{
var logicalNotExpression = (PrefixUnaryExpressionSyntax)expression;
var notToken = logicalNotExpression.OperatorToken;
var nextToken = logicalNotExpression.Operand.GetFirstToken(
includeZeroWidth: true, includeSkipped: true, includeDirectives: true, includeDocumentationComments: true);
var existingTrivia = SyntaxFactory.TriviaList(
notToken.LeadingTrivia.Concat(
notToken.TrailingTrivia.Concat(
nextToken.LeadingTrivia)));
var updatedNextToken = nextToken.WithLeadingTrivia(existingTrivia);
return logicalNotExpression.Operand.ReplaceToken(
nextToken,
updatedNextToken);
}
case SyntaxKind.LogicalOrExpression:
{
var binaryExpression = (BinaryExpressionSyntax)expression;
result = SyntaxFactory.BinaryExpression(
SyntaxKind.LogicalAndExpression,
Negate(binaryExpression.Left, semanticModel, cancellationToken),
SyntaxFactory.Token(
binaryExpression.OperatorToken.LeadingTrivia,
SyntaxKind.AmpersandAmpersandToken,
binaryExpression.OperatorToken.TrailingTrivia),
Negate(binaryExpression.Right, semanticModel, cancellationToken));
return result
.Parenthesize()
.WithLeadingTrivia(binaryExpression.GetLeadingTrivia())
.WithTrailingTrivia(binaryExpression.GetTrailingTrivia());
}
case SyntaxKind.LogicalAndExpression:
{
var binaryExpression = (BinaryExpressionSyntax)expression;
result = SyntaxFactory.BinaryExpression(
SyntaxKind.LogicalOrExpression,
Negate(binaryExpression.Left, semanticModel, cancellationToken),
SyntaxFactory.Token(
binaryExpression.OperatorToken.LeadingTrivia,
SyntaxKind.BarBarToken,
binaryExpression.OperatorToken.TrailingTrivia),
Negate(binaryExpression.Right, semanticModel, cancellationToken));
return result
.Parenthesize()
.WithLeadingTrivia(binaryExpression.GetLeadingTrivia())
.WithTrailingTrivia(binaryExpression.GetTrailingTrivia());
}
case SyntaxKind.TrueLiteralExpression:
{
var literalExpression = (LiteralExpressionSyntax)expression;
return SyntaxFactory.LiteralExpression(
SyntaxKind.FalseLiteralExpression,
SyntaxFactory.Token(
literalExpression.Token.LeadingTrivia,
SyntaxKind.FalseKeyword,
literalExpression.Token.TrailingTrivia));
}
case SyntaxKind.FalseLiteralExpression:
{
var literalExpression = (LiteralExpressionSyntax)expression;
return SyntaxFactory.LiteralExpression(
SyntaxKind.TrueLiteralExpression,
SyntaxFactory.Token(
literalExpression.Token.LeadingTrivia,
SyntaxKind.TrueKeyword,
literalExpression.Token.TrailingTrivia));
}
}
// Anything else we can just negate by adding a ! in front of the parenthesized expression.
// Unnecessary parentheses will get removed by the simplification service.
return SyntaxFactory.PrefixUnaryExpression(
//.........这里部分代码省略.........