简体   繁体   English

PropertyInfo.DeclaringType 真的可以为空吗?

[英]Can PropertyInfo.DeclaringType really ever be null?

I'm using ReSharper (older version), which used to warn me when I use PropertyInfo.DeclaringType that it can be null .我正在使用 ReSharper(旧版本),它曾经在我使用PropertyInfo.DeclaringType时警告我它可以是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 ?是真的吗,还是 ReSharper 有点笨,没有注意到它是PropertyInfo ,而不是一般MemberInfo

The PropertyInfo.DeclaringType property is inherited from MemberInfo.DeclaringType and from documentation : PropertyInfo.DeclaringType属性继承自MemberInfo.DeclaringType文档

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 .如果MemberInfo对象是全局成员(即,如果它是从Module.GetMethods方法获得的,该方法返回模块的全局方法),则返回的DeclaringType将为null

So, warning seems fine in that case.因此,在这种情况下,警告似乎没问题。

It can be null if the property is defined in a module.如果属性是在模块中定义的,则它可以为null In C# you cannot define such methods and properties without reflection (see PropertyBuilder ).在 C# 中,您不能在没有反射的情况下定义此类方法和属性(请参阅PropertyBuilder )。 However, if you reference a VB.NET assembly, it can have such members.但是,如果您引用 VB.NET 程序集,则它可以具有此类成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM