The MemberInfo.GetCustomAttributes method gets the list of member type attributes. By the second parameter, it takes a logical flag, which, if true allows to get a list of attributes, taking into account the entire inheritance chain.

However, the following behavior is mentioned in the description of the method and on MSDN:

This method is ignores the parameter for properties and events. If you’re on the network, you’ll find out how to use it. Attribute.GetCustomAttributes method.

That is, for PropertyInfo and EventInfo logical flag is ignored, only attributes are always returned without regard to the inheritance chain.

What caused this behavior?

  • It is clearly written there: This parameter is ignored for properties and events , that is, properties and events are ignored. I think that this question should be addressed to developers from Microsoft , who wrote this method. - Denis Bubnov
  • one
    @DenisBubnov, well, so the question is exactly why ignoring happens for properties and events - Grundy
  • @DenisBubnov, this is clearly written in my question below the quotation. I think the community may well be aware of, and if not, then maybe someone has some suggestions. On a large SO such questions are asked regularly. - dm.dymov
  • @Grundy, this is written in the MSDN documentation, in the documentation for the method that was written by the Microsoft developers, you and I can only guess and guess why. - Denis Bubnov
  • one

2 answers 2

I do not agree with the answer of Paul.

First, properties are only syntactic sugar around methods + additional metadata. And I do not remember something, that there was some fundamental difference at the level of the CLR from the methods (if there is, then proof in the studio, I will be very happy to learn something new).

Secondly, just look at the implementation of MethodInfo.GetCustomAttribute . There you will see that all the work is done in the CustomAttribute.GetCustomAttributes method in which the value of the inherit parameter is inherit and if it is true , then the attributes of the base class are analyzed.

Now, look at the CustomAttributeExtensions.GetCustomAttributes extension CustomAttributeExtensions.GetCustomAttributes . This method, unlike the instance method MemberInfo.GetCustomAttribute has no restrictions on properties and simply pulls the CustomAttributeExtensions.InternalGetCustomAttribute method which, like in the case of method attributes earlier, simply passes the inheritance hierarchs manually.

Now, to the original question: why doesn't MemberInfo.GetCustomAttributes work for properties? I think that someone initially made a mistake, and such errors cannot be fixed without breaking existing customers. So then I had to add extension methods that corrected the situation and allowed to return the functionality missed initially.

And about the CLR ... It is not true, it seems :))

  • But maybe there was some point in doing just that, and not a simple mistake? For example, is there any optimization there? - Grundy
  • one
    @Grundy I poluznayu from colleagues, but, offhand, I do not see anything like that. All these 'inherited' parameters are a clean add-on. There the manual analysis of the inheritance hierarchy is carried out completely. So perhaps this was simply not necessary. Other reasons are not visible yet. - Sergey Teplyakov
  • @downvoter: just curious, what is the reason for the minus ?;) - Sergey Teplyakov
  • You do not have a minus - Grundy
  • Could you answer the question Why is an empty delegate Action <T> needed? And then the opinions were divided :-) - Grundy

The answer is simple: at the CLR level, properties are not redefined, but hide each other by name. If you have a virtual property in the parent and you redefined it in the descendant, from the CLR point of view these are two different properties.