ReSharper 如何知道此返回类型永远不为 null?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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
. ApparentlyDirectoryInfo.GetFiles
never returnsnull
.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_FrameworkEdit: 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"
正如 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.