.net 是否可以使用Roslyn检测无法访问的代码或其他内置编译警告

.net 是否可以使用Roslyn检测无法访问的代码或其他内置编译警告,.net,roslyn,.net,Roslyn,是否可以使用Roslyn检测无法访问的代码或其他内置编译警告 private void DoSomething() { string a = "TEST"; throw new Exception("Why would I throw an exception here?"); a = "This will never be reached"; //this is a compile time warning for unreachable code...Can

是否可以使用Roslyn检测无法访问的代码或其他内置编译警告

private void DoSomething()
{
     string a = "TEST";

     throw new Exception("Why would I throw an exception here?");

     a = "This will never be reached"; //this is a compile time warning for unreachable code...Can I detect it?

}

我已尝试在语义和语法方法中检查节点属性,但未发现任何问题或警告集合。

您可以使用语义模型的AnalyzerRegionControlFlow方法来发现这一点。您将此传递称为与您感兴趣的语句相对应的文本范围。AnalyzerRegionControlFlow将返回一个具有属性RegionEndPointisRechable的数据结构,它还将告诉您跳入或跳出该区域的所有语句


如果您想知道如何找到编译器将报告的实际诊断,您需要在语义模型上使用GetDiagnostics方法。

谢谢,到目前为止,我在使用Roslyn时还没有看到AnalyzerRegionControl流的用法。效果很好。