ReSharper 如何知道此返回类型永远不为 null?

发布于 2024-09-10 00:13:40 字数 580 浏览 7 评论 0原文

我正在使用 ReSharper 5.0,并且想知道它的代码分析功能如何知道使用注释“表达式始终为假”来突出显示以下 assemblies == null

var directory = new DirectoryInfo("somedir");
FileInfo[] assemblies = directory.GetFiles("*.dll");

if (assemblies == null <<--- this is highlighted with "Expression is always false"
    || assemblies.Length == 0)
{
  _log.Warn("No assemblies found");
}

我知道返回类型是否是值类型,但事实并非如此。我还了解是否有某种代码契约或元数据声明 .GetFiles() 永远不会返回 null。但我认为不存在。

那么 - 它是怎么知道这一点的呢?我是否遗漏了一些明显的东西,或者 ReSharper 是否拥有一些特权知识,例如有关框架方法的元数据的内部列表?或者它实际上“内省”内部代码并解决它?

I'm using ReSharper 5.0, and am wondering how its code analysis function knows to higlight the following assemblies == null with the comment "Expression is always false".

var directory = new DirectoryInfo("somedir");
FileInfo[] assemblies = directory.GetFiles("*.dll");

if (assemblies == null <<--- this is highlighted with "Expression is always false"
    || assemblies.Length == 0)
{
  _log.Warn("No assemblies found");
}

I'd understand if the return type was a value-type, which it isn't. I'd also understand if there was some sort of code contract or metadata stating .GetFiles() will never return null. but I don't think there is.

So - how does it know this? Am I missing something obvious, or does ReSharper have some privileged knowledge, such as an internal list of metadata about framework methods? Or does it actually "introspect" the internal code and work it out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瘫痪情歌 2024-09-17 00:13:40

ReSharper 开发人员对 .NET 框架二进制文件进行流分析,并确定哪些方法可能返回或不返回 null。显然 DirectoryInfo.GetFiles 永远不会返回 null

您可以使用一组 JetBrains. 属性对自己的代码进行注释以指示同一组规则。查看 ReSharper 网站:http://www.jetbrains.com/resharper /features/code_analysis.html#Annotated_Framework

编辑: 具体回答您的问题,“ReSharper 是否拥有一些特权知识,例如有关框架方法的元数据的内部列表” - 是的,它来自“反思内部代码并解决它”

The ReSharper developers ran flow analysis on the .NET framework binaries and determined which methods may or may not return null. Apparently DirectoryInfo.GetFiles never returns null.

You can annotate your own code to indicate the same set of rules, with a set of JetBrains. attributes. Take a look at the ReSharper site: http://www.jetbrains.com/resharper/features/code_analysis.html#Annotated_Framework

Edit: to answer your question specifically, "does ReSharper have some privileged knowledge, such as an internal list of metadata about framework methods" - yes, it came from "introspecting the internal code and working it out"

兔姬 2024-09-17 00:13:40

正如 Tim 指出的,我们对 .NET Framework 进行了注释。它与代码契约类似,但做法略有不同。如果您查看ReSharper安装中的bin文件夹,您可以看到所有注释。

As Tim points out, we annotate the .NET Framework. It's similar to what you get with Code Contracts, but done a little bit differently. If you look under the bin folder in ReSharper installation, you can see all the annotations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文