是否有可能检测不可达code或其它内置使用罗斯林编译警告 [英] Is it possible to detect unreachable code or other built in compile warnings using Roslyn

查看:237
本文介绍了是否有可能检测不可达code或其它内置使用罗斯林编译警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能检测不可达code或其它内置使用罗斯林编译警告?

Is it possible to detect unreachable code or other built in compile warnings using 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?

}

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

I've tried examining the node properties in Semantic and Syntax methods but didn't see any issues or warning collections.

推荐答案

您可以使用语义模型的AnalyzeRegionControlFlow方法发现这一点。你把这个传递一个文本范围对应于您感兴趣的语句。AnalyzeRegionControlFlow会回报你的数据结构有一个属性RegionEndPointIsReachable,它还会告诉你所有的语句要么跳转进入或离开该区域。

You can discover this using the semantic model's AnalyzeRegionControlFlow method. You call this passing in a text span that correspond to the statements you are interested in. AnalyzeRegionControlFlow will return you a data structure that has a property RegionEndPointIsReachable, it will also tell you all the statements that either jump into or out of the region.

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

If you want to know how to find the actual diagnostics that the compiler would report you need to use the GetDiagnostics method on the semantic model.

这篇关于是否有可能检测不可达code或其它内置使用罗斯林编译警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆