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


C# ExpressionSyntax.ReplaceNode方法代码示例

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


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

示例1: SemanticsChangedAsync

        private async Task<bool> SemanticsChangedAsync(
            Document document,
            VariableDeclarationSyntax declaration,
            ExpressionSyntax invocationOrCreation,
            TypeSyntax newType,
            IdentifierNameSyntax identifier,
            DeclarationExpressionSyntax declarationExpression,
            CancellationToken cancellationToken)
        {
            if (newType.IsVar)
            {
                // Options want us to use 'var' if we can.  Make sure we didn't change
                // the semantics of teh call by doing this.

                // Find the symbol that the existing invocation points to.
                var root = await document.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
                var semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);
                var previousSymbol = semanticModel.GetSymbolInfo(invocationOrCreation).Symbol;

                var annotation = new SyntaxAnnotation();
                var updatedInvocationOrCreation = invocationOrCreation.ReplaceNode(
                    identifier, declarationExpression).WithAdditionalAnnotations(annotation);

                // Note(cyrusn): "https://github.com/dotnet/roslyn/issues/14384" prevents us from just
                // speculatively binding the new expression.  So, instead, we fork things and see if
                // the new symbol we bind to is equivalent to the previous one.
                var newDocument = document.WithSyntaxRoot(
                    root.ReplaceNode(invocationOrCreation, updatedInvocationOrCreation));

                var newRoot = await newDocument.GetSyntaxRootAsync(cancellationToken).ConfigureAwait(false);
                var newSemanticModel = await newDocument.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

                updatedInvocationOrCreation = (ExpressionSyntax)newRoot.GetAnnotatedNodes(annotation).Single();

                var updatedSymbol = newSemanticModel.GetSymbolInfo(updatedInvocationOrCreation).Symbol;

                if (!SymbolEquivalenceComparer.Instance.Equals(previousSymbol, updatedSymbol))
                {
                    // We're pointing at a new symbol now.  Semantic have changed.
                    return true;
                }
            }

            return false;
        }
开发者ID:otawfik-ms,项目名称:roslyn,代码行数:45,代码来源:CSharpInlineDeclarationCodeFixProvider.cs

示例2: RewritePostfixUnarys

        protected ExpressionSyntax RewritePostfixUnarys(ExpressionSyntax node)
        {
            var expressions = node.DescendentNodesAndSelf().OfType<PostfixUnaryExpressionSyntax>().ToArray();
            if (expressions.Length == 0)
                return node;

            List<string> names = new List<string> (expressions.Length);
            foreach (var expr in expressions)
            {
                if (expr.Kind != SyntaxKind.PostIncrementExpression && expr.Kind != SyntaxKind.PostDecrementExpression)
                    continue; // These aren't the droids we're looking for.

                IdentifierNameSyntax name = FindIdentifierName (expr);
                if (name == null)
                    continue; // We didn't find a name

                if (names.Contains (name.PlainName))
                {
                    var newExpr = GetLogExpression (name.PlainName, expr);
                    node = node.ReplaceNode (expr, newExpr);
                }
                else
                    names.Add (name.PlainName);
            }

            // We'll use a string builder construct our expression.
            StringBuilder builder = new StringBuilder ("LogPostfixValues ((");
            builder.Append (node.ToString());
            builder.Append ("), ");

            bool found = false;
            foreach (string name in names)
            {
                if (found)
                    builder.Append (", ");

                // We need to specify our generic types so we match
                // the params argument type
                builder.Append ("new Tuple<string, object> (\"");
                builder.Append (name);
                builder.Append ("\", ");
                builder.Append (name);
                builder.Append (")");
                found = true;
            }

            if (!found)
                return node; // We didn't find any usable expressions

            builder.Append (")");

            // Parse and return our new expression.
            return Syntax.ParseExpression (builder.ToString());
        }
开发者ID:Mstrymt,项目名称:Instant,代码行数:54,代码来源:LoggingRewriter.cs


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