Can PropertyInfo.DeclaringType really ever be null?

自闭症网瘾萝莉.ら 提交于 2019-12-08 15:20:40

问题


I'm using ReSharper (older version), which used to warn me when I use PropertyInfo.DeclaringType that it can be null.

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?


回答1:


PropertyInfo.DeclaringType property is inherited from MemberInfo.DeclaringType and from documentation:

If the MemberInfo object is a global member (that is, if it was obtained from the Module.GetMethods method, which returns global methods on a module), the returned DeclaringType will be null.

So, warning seems fine in that case.




回答2:


It can be null if the property is defined in a module. In C# you cannot define such methods and properties without reflection (see PropertyBuilder). However, if you reference a VB.NET assembly, it can have such members.



来源:https://stackoverflow.com/questions/35266010/can-propertyinfo-declaringtype-really-ever-be-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!