Roslyn - CodeFixProvider 正在被解雇,但 DiagnosticAnalyzer 不适用于 class 析构函数

Roslyn - CodeFixProvider is being fired but DiagnosticAnalyzer not for class destructor

我的分析器的伪代码

我的测试class:

public class TestClass
{
     ~TestClass()
     {
     }
}

我的诊断分析仪 class 分析方法:

public class TestClassAnalyzer : DiagnosticAnalyzer
{
     public const string AnalyzerId = "TestId";

     ...

     private static void AnalyzeSymbol(SymbolAnalysisContext context)
     {
           var methodDeclaration = (IMethodSymbol)context.Symbol;

           if (methodDeclaration.MethodKind == MethodKind.Destructor)
           {
               return;
           }

           context.ReportDiagnostic(Diagnostic.Create(...));
     }
}

我的代码修复提供程序使用修复方法:

public class TestClassCodeFixProvider : CodeFixProvider
{
     public sealed override ImmutableArray<string> FixableDiagnosticIds =>
          ImmutableArray.Create(TestClassAnalyzer.AnalyzerId);


     ...

     private async Task<Solution> PerformFixAsync(Document document, ...)
     {
          ...

          return await Renamer.RenameSymbolAsync(...)
     }
}

如果我在我的 TestClassAnalyzer class 中检查析构函数的行之后放置一个断点,那么我的代码永远不会 stops/breaks 这对我来说有意义,因为我用 [= 跳出了方法34=] 关键字。 尽管如此,我的代码修复被触发了(我可以在 PerformFixAnync 方法和代码 stops/breaks 中放置断点)并且我可以看到红色的波浪线。

是否有人知道为什么代码修复被触发,尽管它不应该被触发?

我猜你的分析器 returns 早用于 descructors,因为条件错误:

if (methodDeclaration.MethodKind == MethodKind.Destructor)
{
    return;
}

context.ReportDiagnostic(Diagnostic.Create(...));

在 then 块中否定条件或报告条件:

if (methodDeclaration.MethodKind != MethodKind.Destructor)
{
    return;
}

context.ReportDiagnostic(Diagnostic.Create(...));

事实证明,我的解决方案包含另一个 class 埋在文件夹之间的某个地方,其中包含一些没有析构函数检查条件的旧代码,这导致了麻烦。此 class 不是 TFS 中源代码的事件部分 ...