为什么ReSharper告诉我"User.Identity == null"?会永远是假的吗? [英] Why is ReSharper telling me that "User.Identity == null" will always be false?
问题描述
I have a simple property inside one of my ASP.NET MVC Controller classes.
我以前已经看过很多次了,所以请理解消息的含义,但是通常这很有意义.但是,事实并非如此.要获得带下划线的语句,User
必须不为空,因此对User.Identity
的检查是可以的.
I've seen this many times before, so understand what the message means, but usually it makes perfect sense. This, however, doesn't. To get to the underlined statement, User
would have to NOT be null, so the check for User.Identity
is fine.
Identity
属性是 IPrincipal
接口,并返回继承 IIdentity
.
The Identity
property is part of the IPrincipal
interface, and returns an object that inherits IIdentity
.
要继承此接口或与此相关的任何接口,此属性必须是引用类型,因此可能为null,对吧?
To inherit this interface, or any interface for that matter, this property must be a reference type, and therefore can potentially be null, right?
那我心爱的ReSharper为什么会抱怨呢?
So why is my beloved ReSharper moaning?
推荐答案
您说过,您正在使用GenericPrinciple
作为IPrincipal
的实现.对于此类,Identity
属性的确不能为null
.很容易看出您是否查看了源代码(例如,使用 JetBrains dotPeek ).
You said you are using the GenericPrinciple
as the implementation of IPrincipal
. For this class the Identity
property can indeed never be null
. It is easy to see if you look at the source code (e.g. using JetBrains dotPeek).
为此,您可以感谢ReSharper的.NET Framework类库的代码注释.
You can thank ReSharper's code annotations for the .NET framework class libraries for this.
在我的ReSharper 6.1注释中,有一个与此相关的代码注释(在文件 ExternalAnnotations \ mscorlib \ mscorlib.4.0.0.0.Nullness.Generated.xml 中):
In my ReSharper 6.1 annotations there is this single code annotation related to this (in file ExternalAnnotations\mscorlib\mscorlib.4.0.0.0.Nullness.Generated.xml):
<member name="M:System.Security.Principal.GenericPrincipal.#ctor(System.Security.Principal.IIdentity,System.String[])">
<parameter name="identity">
<attribute ctor="M:JetBrains.Annotations.NotNullAttribute.#ctor" />
</parameter>
</member>
这只是用于构造函数,我还没有为Identity
属性找到一个.因此,或者您正在使用的ReSharper版本也对该属性进行了注释,或者ReSharper在进行其他分析.
This is just for the contructor though, I haven't found one for the Identity
property. So either you are using a ReSharper version that has annotation for that property too or ReSharper is doing some additional analysis.
在任何情况下,都是ReSharper聪明(对!).
这篇关于为什么ReSharper告诉我"User.Identity == null"?会永远是假的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!