PropertyInfo.DeclaringType 真的可以为空吗? [英] Can PropertyInfo.DeclaringType really ever be null?
问题描述
我正在使用 ReSharper(旧版本),它曾经在我使用 PropertyInfo.DeclaringType
时警告我它可以是 null
.
I'm using ReSharper (older version), which used to warn me when I use PropertyInfo.DeclaringType
that it can be null
.
乍一看,这对我来说毫无意义.是真的吗,还是 ReSharper 有点笨,没有注意到它是一个 PropertyInfo
,而不是一个通用的 MemberInfo
?
It doesn't make sense to me at first and second glances. Is it really true, or is ReSharper a bit dumb and doesn't notice that it's a PropertyInfo
, not a general MemberInfo
?
推荐答案
PropertyInfo.DeclaringType
属性继承自 MemberInfo.DeclaringType
和 文档:
The PropertyInfo.DeclaringType
property is inherited from MemberInfo.DeclaringType
and from documentation:
如果 MemberInfo
对象是一个全局成员(也就是说,如果它是从 Module.GetMethods
方法获取,该方法返回全局模块上的方法),返回的 DeclaringType
将为 null
.
If the
MemberInfo
object is a global member (that is, if it was obtained from theModule.GetMethods
method, which returns global methods on a module), the returnedDeclaringType
will benull
.
所以,在这种情况下,警告似乎没问题.
So, warning seems fine in that case.
这篇关于PropertyInfo.DeclaringType 真的可以为空吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!