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


C# CodeFixContext.RegisterFix方法代码示例

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


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

示例1: ComputeFixesAsync

        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
                             
            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            var token = root.FindToken(diagnosticSpan.Start);

            if (token.IsKind(SyntaxKind.CatchKeyword))
            {
                var catchBlock = (CatchClauseSyntax)token.Parent;
                var tryStmt = (TryStatementSyntax)catchBlock.Parent;

                var throwStatement = SyntaxFactory.ThrowStatement();
                var newStatements = new SyntaxList<StatementSyntax>().Add(throwStatement);
                var newBlock = SyntaxFactory.Block().WithStatements(newStatements);
                var newCatchBlock = SyntaxFactory.CatchClause().WithBlock(newBlock).WithAdditionalAnnotations(Formatter.Annotation);

                var newRoot = root.ReplaceNode(catchBlock, newCatchBlock);

                context.RegisterFix(
                    CodeAction.Create("throw", context.Document.WithSyntaxRoot(newRoot)),
                    diagnostic);
            }
        }
开发者ID:julid29,项目名称:confsamples,代码行数:26,代码来源:CodeFixProvider.cs

示例2: ComputeFixesAsync

        public override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            var declaration = root.FindToken(diagnosticSpan.Start).Parent
                .FirstAncestorOrSelf<AwaitExpressionSyntax>();

            context.RegisterFix(
                CodeAction.Create("Add ConfigureAwait(false)", cancellationToken =>
                    AddConfigureAwait(context.Document, declaration, false, cancellationToken)),
                diagnostic);
            context.RegisterFix(
                CodeAction.Create("Add ConfigureAwait(true)", cancellationToken =>
                    AddConfigureAwait(context.Document, declaration, true, cancellationToken)),
                diagnostic);
        }
开发者ID:aelij,项目名称:ConfigureAwaitChecker,代码行数:18,代码来源:ConfigureAwaitCSharpCodeFix.cs

示例3: ComputeFixesAsync

        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<LocalDeclarationStatementSyntax>().First();

            context.RegisterFix(CodeAction.Create("Make constant", c => MakeConstAsync(context.Document, declaration, c)), diagnostic);
        }
开发者ID:julid29,项目名称:confsamples,代码行数:11,代码来源:CodeFixProvider.cs

示例4: ComputeFixesAsync

		public sealed override async Task ComputeFixesAsync(CodeFixContext context)
		{
			var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

			var diagnostic = context.Diagnostics.First();
			var diagnosticSpan = diagnostic.Location.SourceSpan;

			// Find the type declaration identified by the diagnostic.
			var declaration = root.FindToken(diagnosticSpan.Start).Parent;

			// Register a code action that will invoke the fix.
			context.RegisterFix(
				CodeAction.Create("Make lowercase", c => MakeLowercaseAsync(context.Document, declaration, c)),
				diagnostic);
		}
开发者ID:modulexcite,项目名称:CRMNamingAnalyzer,代码行数:15,代码来源:BracketedArgumentListCodeFix.cs

示例5: ComputeFixesAsync

        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            // TODO: Replace the following code with your own analysis, generating a CodeAction for each fix to suggest
            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            // Find the type declaration identified by the diagnostic.
            var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<TypeDeclarationSyntax>().First();

            // Register a code action that will invoke the fix.
            context.RegisterFix(
                CodeAction.Create("Make uppercase", c => MakeUppercaseAsync(context.Document, declaration, c)),
                diagnostic);
        }
开发者ID:calebjenkins,项目名称:DotNetAnalyzers,代码行数:16,代码来源:CodeFixProvider.cs

示例6: ComputeFixesAsync

        public sealed override async Task ComputeFixesAsync(CodeFixContext context)
        {
            var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

            var diagnostic = context.Diagnostics.First();
            var diagnosticSpan = diagnostic.Location.SourceSpan;

            // Find the type declaration identified by the diagnostic.
            var declaration = root.FindToken(diagnosticSpan.Start).Parent.AncestorsAndSelf().OfType<ConstructorDeclarationSyntax>().First();

            var previousWhiteSpacesToken = SyntaxFactory.Token(declaration.GetLeadingTrivia(), SyntaxKind.StringLiteralToken, SyntaxTriviaList.Empty);
            var newList = SyntaxTokenList.Create(previousWhiteSpacesToken);

            var newDeclaration = declaration.WithModifiers(newList).
                AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword)).
                AddModifiers(SyntaxFactory.Token(SyntaxTriviaList.Create(SyntaxFactory.Space), SyntaxKind.StringLiteralToken, SyntaxTriviaList.Empty));

            // Register a code action that will invoke the fix.
                context.RegisterFix(
                CodeAction.Create("Make constructor protected", c => ReplaceConstructorInDocumentAsync(context.Document, declaration, newDeclaration, c)),
                diagnostic);
        }
开发者ID:zanyants,项目名称:Strathweb.Roslyn,代码行数:22,代码来源:CodeFixProvider.cs


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