开发者

strange Resharper warning on MemberInfo.DeclaringType

开发者 https://www.devze.com 2023-02-27 10:34 出处:网络
Resharper tells me that MemberInfo.DeclaringType can never be null: However when that code is run, the text \"Top 开发者_Go百科level member\" is printed. I don\'t get it, what\'s wrong here? Microso

Resharper tells me that MemberInfo.DeclaringType can never be null:

strange Resharper warning on MemberInfo.DeclaringType

However when that code is run, the text "Top 开发者_Go百科level member" is printed. I don't get it, what's wrong here?


Microsoft Code Contracts states that it is never null.

// System.Reflection.MemberInfo
public virtual Type DeclaringType
{
    get
    {
        Contract.Ensures(Contract.Result<Type>() != null, null, "Contract.Result<Type>() != null");
        Type result;
        return result;
    }
}

So ReSharper relies on Code Contracts here.


Resharper is simply wrong here. MemberInfo is an abstract type and it's possible for an arbitrary implementation to return whatever it pleases including null

Example:

class EvilMemberInfo : MemberInfo
{
    public override System.Type DeclaringType
    {
        get { return null; }
    }

    // Rest omitted for brevity
}
0

精彩评论

暂无评论...
验证码 换一张
取 消