Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strange Resharper warning on MemberInfo.DeclaringType

Resharper tells me that MemberInfo.DeclaringType can never be null: enter image description here

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

like image 230
JBSnorro Avatar asked Apr 21 '11 17:04

JBSnorro


2 Answers

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.

like image 114
Evgeny Pasynkov Avatar answered Nov 10 '22 01:11

Evgeny Pasynkov


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
}
like image 27
JaredPar Avatar answered Nov 10 '22 00:11

JaredPar