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


C# Document.GetAnnotatedInvocation方法代码示例

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


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

示例1: RefactorToTask

        /// <summary>
        /// Execute the APM-to-async/await refactoring for a given APM method invocation.
        /// </summary>
        /// <param name="document">The C# Document on which to operate/in which the Begin and End method calls are represented.</param>
        /// <param name="solution">The solution that contains the C# document.</param>
        /// <param name="workspace">The workspace to which the code in the syntax tree currently belongs, for formatting purposes.</param>
        /// <param name="index">The index number </param>
        /// <returns>The CompilationUnitSyntax node that is the result of the transformation.</returns>
        public static CompilationUnitSyntax RefactorToTask(Document document, Solution solution, Workspace workspace, int index)
        {
            if (document == null) throw new ArgumentNullException("document");
            if (workspace == null) throw new ArgumentNullException("workspace");

            String message;

            var numErrorsInSolutionBeforeRewriting = solution.CompilationErrorCount();

            var syntaxTree = (SyntaxTree)document.GetSyntaxTreeAsync().Result;
            var syntax = (CompilationUnitSyntax)syntaxTree.GetRoot();

            Logger.Trace("\n### REFACTORING CODE ###\n{0}\n### END OF CODE ###", syntax.Format(workspace));

            InvocationExpressionSyntax beginXxxCall;
            try
            {
                beginXxxCall = document.GetAnnotatedInvocation(index);
            }
            catch (InvalidOperationException)
            {
                throw new ArgumentException(
                    "Syntax tree has no InvocationExpressionSyntax node annotated with RefactorableAPMInstance");
            }

            var model = (SemanticModel)document.GetSemanticModelAsync().Result;

            var callbackArgument = FindAsyncCallbackInvocationArgument(model, beginXxxCall);
            var callbackExpression = callbackArgument.Expression;

            CompilationUnitSyntax rewrittenSyntax;
            switch (callbackExpression.CSharpKind())
            {
                case SyntaxKind.SimpleLambdaExpression:
                    var lambda = (SimpleLambdaExpressionSyntax)callbackExpression;

                    switch (lambda.Body.CSharpKind())
                    {
                        case SyntaxKind.Block:
                            var stateArgument = FindAsyncStateInvocationArgument(model, beginXxxCall);

                            switch (stateArgument.Expression.CSharpKind())
                            {
                                case SyntaxKind.NullLiteralExpression:
                                    Logger.Info("Refactoring:\n{0}", beginXxxCall.ContainingMethod());

                                    return RefactorSimpleLambdaInstance(syntax, beginXxxCall, model, workspace, callbackArgument);

                                default:
                                    Logger.Info("Rewriting to remove state argument:\n{0}", beginXxxCall);

                                    rewrittenSyntax = RewriteStateArgumentToNull(lambda, syntax, stateArgument);

                                    break;
                            }

                            break;

                        case SyntaxKind.InvocationExpression:
                            Logger.Info("Rewriting lambda to block form:\n{0}", beginXxxCall);

                            rewrittenSyntax = RewriteInvocationExpressionToBlock(syntax, lambda, model, beginXxxCall);
                            break;

                        default:
                            message = String
                                .Format(
                                    "Unsupported lambda body kind: {0}: method:\n{1}",
                                    lambda.Body.CSharpKind(),
                                    beginXxxCall.ContainingMethod()
                                );

                            Logger.Error("Not implemented: {0}", message);

                            throw new NotImplementedException(message);
                    }

                    break;

                case SyntaxKind.IdentifierName:
                case SyntaxKind.SimpleMemberAccessExpression:
                    Logger.Info("Rewriting method reference to lambda:\n{0}", beginXxxCall);

                    rewrittenSyntax = RewriteMethodReferenceToSimpleLambda(syntax, beginXxxCall, model, callbackArgument, callbackExpression);
                    break;

                case SyntaxKind.ParenthesizedLambdaExpression:
                    Logger.Info("Rewriting parenthesized lambda to simple lambda:\n{0}", beginXxxCall);

                    rewrittenSyntax = RewriteParenthesizedLambdaToSimpleLambda(syntax, beginXxxCall, model);
                    break;

//.........这里部分代码省略.........
开发者ID:modulexcite,项目名称:concurrent-code-analyses,代码行数:101,代码来源:Taskifier.cs


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