c# - 部分类的 ReportDiagnostic

标签 c# visual-studio-2015 roslyn

我正在修改来自代码分析器模板的默认分析器项目,以尝试让它报告分部类的所有声明。

我已将代码修改为:

public override void Initialize(AnalysisContext context)
{
    context.RegisterSymbolAction(AnalyzeSymbol, SymbolKind.NamedType);
}

private static void AnalyzeSymbol(SymbolAnalysisContext context)
{
    var namedTypeSymbol = (INamedTypeSymbol)context.Symbol;

    // Find just those named type symbols with names containing lowercase letters.
    if (namedTypeSymbol.Name.ToCharArray().Any(char.IsLower))
    {
        foreach (var location in namedTypeSymbol.Locations)
        {
            // For all such symbols, produce a diagnostic.
            var diagnostic = Diagnostic.Create(Rule, location, namedTypeSymbol.Name);
            context.ReportDiagnostic(diagnostic);
        }
    }
}

在两个单独的文件中,我有这样的部分类:

// File1.cs
partial class Foo
{
    public string BarString;
}

// File2.cs
partial class Foo
{
    public string FooBarString;
}

我在 ReportDiagnostic 上放置了断点,我看到它针对每个位置调用,但在 Visual Studio 中它只报告单个文件中的诊断信息。

如果我将 Foo 的多个实现放在一个文件中(它恰好报告了该文件声明),那么我将看到两个诊断报告。

我是否误解了应该如何报告诊断信息,或者这是一个错误?如果是bug,是Roslyn的问题还是Visual Studio消费Roslyn的问题?

最佳答案

这是 Visual Studio 诊断服务的 V1 实现的限制。

Roslyn 存储库中有一个问题可以跟踪此问题:

https://github.com/dotnet/roslyn/issues/3748#issuecomment-117231706

来自Github issue中的回复:

This is a known issue in the v1 implementation of the Visual Studio IDE's diagnostic service. It currently doesn't handle analyzer reporting diagnostics outside the document being analyzed. So, if File1.cs has the primary definition of Foo for which AnalyzeSymbol was invoked, then the diagnostic service only retains diagnostics reported by the analyzer within this file.

关于c# - 部分类的 ReportDiagnostic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30856482/

相关文章:

c# - 将 2 个带有内部列表的字典与 Diff 字典进行比较?

c# - 如何取消引用 LINQ 中可能为空的字符串

C# ASP.NET、WebForms 到 MVC : Does it make sense to change in our case?

c++ - 错误:数组下标高于 std::vector::insert 的数组边界

tfs - Nuget Update-Package 命令非常慢

database - Visual Studio/IIS改变Access文件表数据的顺序

c# - QueryString 未正确解析

c# - 删除 block 的内容缩进

c# - 使用 SyntaxRewriter 删除代码中的冗余分号

c# - MS2015 中的 MvcBuildViews 需要很长时间