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

时间:2012-04-19 17:30:11

标签: .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 I detect it?

}

我尝试在语义和语法方法中检查节点属性,但没有看到任何问题或警告集合。

1 个答案:

答案 0 :(得分:7)

您可以使用语义模型的AnalyzeRegionControlFlow方法发现这一点。您可以在与您感兴趣的语句相对应的文本范围内调用此传递.AnalyzeRegionControlFlow将返回一个具有RegionEndPointIsReachable属性的数据结构,它还会告诉您跳入或跳出该区域的所有语句。 / p>

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