为什么Resharper认为IPrincipal.Identity永远不会为空?

Dyl*_*tie 11 c# resharper iprincipal

在VS2010中运行的Resharper 8告诉我我可以删除支票principal.Identity != null:

在此输入图像描述

我假设这是因为有一个NotNull属性或潜伏在IPrincipal的代码中的东西,但是编写自己的IPrincipal实现很容易返回null身份:

void Main() {
    var foo = new FooPrincipal();
    Console.WriteLine(foo.Identity == null ? "Yep!" : "Not Null");
}

class FooPrincipal : IPrincipal {
    public IIdentity Identity { get; set; }
    public bool IsInRole(string role) { return(false); }
    public FooPrincipal() {}
}
Run Code Online (Sandbox Code Playgroud)

Resharper如何知道传入此方法的IPrincipal不会成为我返回null身份的FooPrincipals之一?

编辑:好的,这是一个完整的复制案例,Resharper实际上鼓励你编写在生产中爆炸的代码......

using System;
using System.Security.Principal;

namespace ResharperPrincipalTest {
    class Program {
        static void Main(string[] args) {
            var p = new TestPrincipal();
            var isJeff = IsUserCalledJeff(p);
            Console.WriteLine(isJeff);
        }

        static bool IsUserCalledJeff(IPrincipal principal) {
            if (principal != null) {
                if (principal.Identity == null) throw(new Exception("Resharper says this will never happen!"));
                return (principal.Identity.Name == "jeff");
            }
            return false;
        }
    }

    class TestPrincipal : IPrincipal {
        public bool IsInRole(string role) {
            return (false);
        }

        public IIdentity Identity { get; set; }
    }
}
Run Code Online (Sandbox Code Playgroud)

VS2010的屏幕截图显示了Resharper的"有用"突出显示......

在此输入图像描述

当然,当你点击F5时,程序会抛出一个异常.我会说这是对我原来问题的答案"因为Resharper是错的":)

编辑2:http://youtrack.jetbrains.com/issue/RSRP-398551提交的Resharper错误报告

JRO*_*JRO 0

Resharper 预计如果执行该方法,则参数"IPrincipal principal"不为 null,因此在 Resharpers Eyes 中检查 !=null 将是绝对的

Resharper 无法知道您是否正在"FooPrincipal"向方法发送作为参数。