当前位置: 首页>>代码示例>>C#>>正文


C# IfStatementSyntax.ReplaceNode方法代码示例

本文整理汇总了C#中IfStatementSyntax.ReplaceNode方法的典型用法代码示例。如果您正苦于以下问题:C# IfStatementSyntax.ReplaceNode方法的具体用法?C# IfStatementSyntax.ReplaceNode怎么用?C# IfStatementSyntax.ReplaceNode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IfStatementSyntax的用法示例。


在下文中一共展示了IfStatementSyntax.ReplaceNode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddBracesAsync

 private async Task<Document> AddBracesAsync(Document document, IfStatementSyntax ifStatement, CancellationToken cancellationToken)
 {
     var nonBlockedStatement = ifStatement.Statement as ExpressionStatementSyntax;
     var newBlockedStatement = SyntaxFactory.Block(statements: nonBlockedStatement).WithAdditionalAnnotations(annotations: Formatter.Annotation);
     var newIfStatement = ifStatement.ReplaceNode(oldNode: nonBlockedStatement, newNode: newBlockedStatement);
     var root = await document.GetSyntaxRootAsync();
     var newRoot = root.ReplaceNode(oldNode: ifStatement, newNode: newIfStatement);
     var newDocument = document.WithSyntaxRoot(newRoot);
     return newDocument;
 }
开发者ID:deepak2007in,项目名称:CSharp6,代码行数:10,代码来源:CodeFixProvider.cs

示例2: HandleNegatedCase

        internal static IEnumerable<CodeAction> HandleNegatedCase(SemanticModel ctx, Document document, SyntaxNode root, IfStatementSyntax ifElseStatement, ExpressionSyntax c2, BinaryExpressionSyntax isExpression, out int foundCastCount)
        {
            foundCastCount = 0;

            var condition = isExpression;
            var castToType = isExpression.Right;
            var embeddedStatment = ifElseStatement.Statement;

            var rr = ctx.GetTypeInfo(castToType);
            if (rr.Type == null || !rr.Type.IsReferenceType)
                return Enumerable.Empty<CodeAction>();

            List<SyntaxNode> foundCasts;

            SyntaxNode searchStmt = embeddedStatment;
            if (IsControlFlowChangingStatement(searchStmt))
            {
                searchStmt = ifElseStatement.Parent;
                foundCasts = searchStmt.DescendantNodesAndSelf(n => !IsCast(ctx, n, rr.Type)).Where(arg => arg.SpanStart >= ifElseStatement.SpanStart && IsCast(ctx, arg, rr.Type)).ToList();
                foundCasts.AddRange(ifElseStatement.Condition.DescendantNodesAndSelf(n => !IsCast(ctx, n, rr.Type)).Where(arg => arg.SpanStart > isExpression.Span.End && IsCast(ctx, arg, rr.Type)));
            }
            else
            {
                foundCasts = new List<SyntaxNode>();
            }

            foundCastCount = foundCasts.Count;

            return new[] {
                CodeActionFactory.Create(
                    isExpression.Span,
                    DiagnosticSeverity.Info,
                    GettextCatalog.GetString ("Use 'as' and check for null"),
                    t2 => {
                        var varName = ReplaceAutoPropertyWithPropertyAndBackingFieldCodeRefactoringProvider.GetNameProposal(RefactoringHelpers.GuessNameFromType(rr.Type), ctx, condition);

                        var varDec = SyntaxFactory.LocalDeclarationStatement(
                            SyntaxFactory.VariableDeclaration(
                                SyntaxFactory.ParseTypeName("var"),
                                SyntaxFactory.SeparatedList(new [] {
                                    SyntaxFactory.VariableDeclarator(varName)
                                        .WithInitializer(SyntaxFactory.EqualsValueClause(
                                            SyntaxFactory.BinaryExpression(SyntaxKind.AsExpression, condition.Left, condition.Right)
                                        ))
                                    })
                            ));
                        //var outerIs = isExpression.AncestorsAndSelf().FirstOrDefault(e => !(e.Parent is ParenthesizedExpressionSyntax));
                        var binaryOperatorExpression = SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression, SyntaxFactory.IdentifierName(varName), SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression));
                        SyntaxNode newRoot;
                        if (IsEmbeddedStatement(ifElseStatement)) {
                            var newIf = ifElseStatement.ReplaceNode((SyntaxNode)c2, binaryOperatorExpression.WithAdditionalAnnotations(Formatter.Annotation));

                            foreach (var c in foundCasts) {
                                newIf = newIf.ReplaceNode((SyntaxNode)newIf.GetCurrentNode(c), SyntaxFactory.IdentifierName(varName).WithAdditionalAnnotations(Formatter.Annotation));
                            }

                            var block = SyntaxFactory.Block(new StatementSyntax[] {
                                varDec,
                                newIf
                            });

                            newRoot = root.ReplaceNode((SyntaxNode)ifElseStatement, block.WithAdditionalAnnotations(Formatter.Annotation));
                        } else {
                            newRoot = root.TrackNodes(foundCasts.Concat(new SyntaxNode[] { ifElseStatement, c2 }) );
                            newRoot = newRoot.InsertNodesBefore(newRoot.GetCurrentNode(ifElseStatement), new [] { varDec.WithAdditionalAnnotations(Formatter.Annotation) });
                            newRoot = newRoot.ReplaceNode((SyntaxNode)newRoot.GetCurrentNode(c2), binaryOperatorExpression.WithAdditionalAnnotations(Formatter.Annotation));
                            foreach (var c in foundCasts) {
                                newRoot = newRoot.ReplaceNode((SyntaxNode)newRoot.GetCurrentNode(c), SyntaxFactory.IdentifierName(varName).WithAdditionalAnnotations(Formatter.Annotation));
                            }
                        }

                        return Task.FromResult(document.WithSyntaxRoot(newRoot));
                    }
                )
            };
        }
开发者ID:Kavignon,项目名称:RefactoringEssentials,代码行数:76,代码来源:UseAsAndNullCheckCodeRefactoringProvider.cs


注:本文中的IfStatementSyntax.ReplaceNode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。