Roslyn 分析器代码修复 - 禁用预览选项 [英] Roslyn analyser code fix - Disable preview option

查看:43
本文介绍了Roslyn 分析器代码修复 - 禁用预览选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何禁用 C# 项目中灯泡后显示的预览对话框?

How do I disable the preview dialog that shows up after the light bulb in C# project?

我遇到的问题是,RegisterCodeFixesAsync 调用数据库并增加 id,这将完成两次(一次在预览期间,第二次在调用操作时),而不是只增加一次,id 增加两次

Problem I have is, the RegisterCodeFixesAsync makes a call to database and increments the id and this is getting done twice (once during the preview and second time when the action is invoked), instead of incrementing just once, id increments twice

推荐答案

CodeAction 有单独的 ComputePreviewOperationsAsync()ComputeOperationsAsync().让它们返回不同的值是我相信你正在寻找的.但是如果您使用调用 CodeAction.Create() 的常用方法,两者将返回相同的值.

CodeAction has separate ComputePreviewOperationsAsync() and ComputeOperationsAsync(). Having them return different values is what I believe you're looking for. But if you use the common approach of calling CodeAction.Create(), both will return the same values.

您可以做的是创建一个自定义类,该类继承自 CodeAction 并以您想要的方式覆盖方法.例如:

What you can do instead is to create a custom class that inherits from CodeAction and overrides the methods the way you want. For example:

class NoPreviewCodeAction : CodeAction
{
    private readonly Func<CancellationToken, Task<Solution>> createChangedSolution;

    public override string Title { get; }

    public override string EquivalenceKey { get; }

    public NoPreviewCodeAction(
        string title, Func<CancellationToken, Task<Solution>> createChangedSolution,
        string equivalenceKey = null)
    {
        this.createChangedSolution = createChangedSolution;

        Title = title;
        EquivalenceKey = equivalenceKey;
    }

    protected override Task<IEnumerable<CodeActionOperation>> ComputePreviewOperationsAsync(
        CancellationToken cancellationToken)
    {
        return Task.FromResult(Enumerable.Empty<CodeActionOperation>());
    }

    protected override Task<Solution> GetChangedSolutionAsync(
        CancellationToken cancellationToken)
    {
        return createChangedSolution(cancellationToken);
    }
}

此版本完全禁用预览.另一种选择是使预览采用不同的路径,例如查询数据库中的下一个值,但不更新它.

This version completely disables preview. Another option would be to make preview take a different path, e.g. querying the database for the next value, but not updating it.

这篇关于Roslyn 分析器代码修复 - 禁用预览选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆