Reputation: 6726
Resharper tells me that MemberInfo.DeclaringType can never be null:
However when that code is run, the text "Top level member" is printed. I don't get it, what's wrong here?
Upvotes: 7
Views: 624
Reputation: 693
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.
Upvotes: 10
Reputation: 754685
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
}
Upvotes: 6