When trying to get properties accessors from derived properties or use CanRead / CanWrite, for some reason base auto-properties are not taken into account.
Can
I expected last two tests to pass, what am I missing?
For a definitive answer, you'd have to ask the people who originally designed .NET and its type system. That said…
It seems to me that this is consistent with the goal of reflection providing information about how a type was written. Consider the alternative: what if the PropertyInfo
object returned included both the setter from the derived class and the getter from the base class. It would be considerably more difficult to understand from the returned result what was actually declared where, and the PropertyInfo
object itself would arguably be inconsistent. This is because there is the PropertyInfo.DeclaringType
property which implies that all of the information for the member pertains just to that declaring type.
With members which are neither properties nor events (both of which encapsulate a pair of class members), you get the behavior you expected. Unless of course you pass BindingFlags.DeclaredOnly
, which restricts the returned information to the declaring type. But in the case of those types of members, the DeclaringType
property tells you unequivocally in which type the member was actually declared.
With a property, the DeclaringType
tells you in which class the property was declared. And then the SetMethod
and GetMethod
properties tell you what that class declared.
IMHO, this makes the reflection API simpler, more consistent, and easier to understand. It does mean that you have to do a little more work to analyze virtual properties. But then, reflection is always going to involve "a little more work". :)