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


C# CompilationUnitSyntax.ReplaceAll方法代码示例

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


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

示例1: RefactorSimpleLambdaInstance

        public static CompilationUnitSyntax RefactorSimpleLambdaInstance(CompilationUnitSyntax syntax, InvocationExpressionSyntax apmSyntax, IMethodSymbol apmSymbol, SimpleLambdaExpressionSyntax lambda)
        {
            if (lambda.Body.CSharpKind() != SyntaxKind.Block)
                throw new NotImplementedException("Lambda body must be rewritten as BlockSyntax - it is now: " + lambda.Body.CSharpKind() + ": lambda: " + lambda);

            var lambdaBlock = (BlockSyntax)lambda.Body;

            var originatingMethodSyntax = apmSyntax.FirstAncestorOrSelf<MethodDeclarationSyntax>();

            // TODO: This precondition'i Analyzer'a koy!!! 
            if (originatingMethodSyntax == null || !originatingMethodSyntax.ReturnsVoid())
            {
                throw new Exception("PRECONDITION: Initiating method does not return void");
            }

            // TODO: Look up the symbol to check that it actually exists.
            var methodNameBase = GetAsyncMethodNameBase(apmSyntax);

            var endStatement = TryFindEndAPMCallSyntaxNode(lambdaBlock, methodNameBase);

            if (endStatement != null)
            {
                return RewriteNotNestedInstance(syntax, originatingMethodSyntax, apmSyntax, lambdaBlock, endStatement, methodNameBase, );
            }

            // Every method invocation might lead to the target EndXxx. Try to find it recursively.
            // Once found, rewrite the methods in the invocation path, one by one.
            // Finally, rewrite the originating method, and the method with the EndXxx statement.

            var invocationPathToEndXxx = TryFindCallGraphPathToEndXxx(lambdaBlock, methodNameBase, model);

            if (invocationPathToEndXxx.Count == 0)
            {
                throw new PreconditionException("Could not find End call in lambda body call graph");
            }

            // These two get special treatment.
            var initialCall = invocationPathToEndXxx.RemoveLast();
            var endXxxCall = invocationPathToEndXxx.RemoveFirst();

            IMethodSymbol endXxxMethod;
            try
            {
                endXxxMethod = model.LookupMethodSymbol(endXxxCall);
            }
            catch (SymbolMissingException e)
            {
                Logger.Error("No symbol found for APM End invocation: {0}", endXxxCall, e);

                throw new RefactoringException("No symbol found for APM End invocation: " + endXxxCall, e);
            }

            var taskTypeParameter = endXxxMethod.ReturnType.Name;

            var replacements = new List<SyntaxReplacementPair>(invocationPathToEndXxx.Count + 2);

            // Replace all intermediate methods on the call graph path.
            replacements.AddRange(
                invocationPathToEndXxx.Select(
                    invocation => new SyntaxReplacementPair(
                        invocation.ContainingMethod(),
                        RewriteCallGraphPathComponent(invocation, taskTypeParameter)
                    )
                )
            );

            // Replace method that contains BeginXxx call.
            var taskName = FreeTaskName(originatingMethodSyntax);
            replacements.Add(
                new SyntaxReplacementPair(
                    originatingMethodSyntax,
                    RewriteOriginatingMethod(
                        apmSyntax,
                        RewriteOriginatingMethodLambdaBlock(lambda, initialCall, taskName),
                        methodNameBase,
                        taskName
                    )
                )
            );

            // Replace method that contains the EndXxx call.
            replacements.Add(
                new SyntaxReplacementPair(
                    endXxxCall.ContainingMethod(),
                    RewriteEndXxxContainingMethod(
                        endXxxCall,
                        taskTypeParameter
                    )
                )
            );

            return syntax
                .ReplaceAll(replacements)
                .WithUsingSystemThreadingTasks()
                .Format(workspace);
        }
开发者ID:modulexcite,项目名称:Asyncifier,代码行数:96,代码来源:CodeRefactoringProvider.cs


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