简体   繁体   English

罗斯林-CodeFixProvider被解雇,但DiagnosticAnalyzer不用于类析构函数

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

Pseudo-code for my analyser 分析仪的伪代码

My test class: 我的测试课:

public class TestClass
{
     ~TestClass()
     {
     }
}

My diagnostic analyzer class with analyze method: 我的带有分析方法的诊断分析器类:

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(...));
     }
}

My code fix provider with fix method: 我的代码修复提供程序具有修复方法:

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(...)
     }
}

If I put a breakpoint after the line with check for destructor inside my TestClassAnalyzer class, then my code never stops/breaks which makes sense to me because I jump out of the method with return keyword. 如果我在TestClassAnalyzer类中的带检查析构函数的行后放置一个断点,那么我的代码将永不停止/中断,这对我来说很有意义,因为我跳出了带有return关键字的方法。 Nevertheless, my code fix is being fired (I can put breakpoint inside the PerformFixAnync method and code stops/breaks there) and I can see red squiggly. 不过,我的代码修复程序已被触发(我可以将断点放在PerformFixAnync方法内,并在其中停止/中断代码),并且可以看到红色的波浪状。

CodeFixProvider

Does anybody have any idea why the code fix is being fired although it shouldn't? 有人知道为什么不启动代码修复程序吗?

I guess your analyzer returns early for descructors as the condition is wrong: 我猜您的分析仪由于条件错误而早日返回了描述者:

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

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

negate the condition or report the condition in the then-block: 在条件块中否定条件或报告条件:

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

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

It turned out that my solution contained another class buried somewhere between folders with some old code without destructor check condition and this was causing the troubles. 事实证明,我的解决方案包含一个埋在文件夹之间某个地方的类,该类中带有一些没有析构函数检查条件的旧代码,这引起了麻烦。 This class was not event part of the source code in TFS ... 此类不是TFS中源事件的一部分...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM