如何ReSharper的知道这个返回类型不能为null? [英] How does ReSharper know this return type is never null?

查看:129
本文介绍了如何ReSharper的知道这个返回类型不能为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ReSharper的5.0,我想知道如何将其code分析功能明知higlight以下组件== NULL 的意见前pression永远是假的。

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");
}

我会明白,如果返回类型是值类型,它不是。我也想知道,如果有是code合同或元数据的某种说明 .GetFiles()将永远不会返回null。但我不觉得有什么。

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.

所以 - 它是怎么知道的?我失去了一些东西很明显,还是ReSharper的具有一定特权的知识,如元数据的约框架方法内部列表?还是它实际上是反思的内部code和它的工作呢?

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?

推荐答案

ReSharper的开发商跑流量分析在.NET框架的二进制文件,并确定哪些方法可能会或可能不会返回。显然 DirectoryInfo.GetFiles 永远不会返回

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.

您可以标注自己的code来表示同一套规则,有一组 JetBrains公司。属性。看看ReSharper的网站:<一href="http://www.jetbrains.com/resharper/features/$c$c_analysis.html#Annotated_Framework">http://www.jetbrains.com/resharper/features/$c$c_analysis.html#Annotated_Framework

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

编辑:具体回答你的问题,没有ReSharper的具有一定特权的知识,比如关于框架方法的元数据的内部名单 - 是的,它是从哪里来的自省内部code和工作出来

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"

这篇关于如何ReSharper的知道这个返回类型不能为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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