views:

20

answers:

1

The MSDN Documentation: MemberInfo.GetCustomAttibutes Method (Type, Boolean) states in the remarks:

This method ignores the inherit parameter for properties and events. To search the inheritance chain for attributes on properties and events, use the appropriate overloads of the Attribute.GetCustomAttributes method.

This basically means that this implementation's second parameter (bool inherit) is ignored for event members and property members. However, calling the Attribute.GetCustomAttributes(MemberInfo,Type,bool) variety of this operation does not.

What puzzles me is the design of this.

Why would they seemingly ignore the inherit attribute arbitrarily on 2 forms of member types?

If anyone could shed some light on this I'd be greatly appreciative.

+1  A: 

This is definitely confusing. The strict answer is that properties and events are not inherited so the inherited parameter has no meaning. The reference is ECMA 335 CLI Specification section 8.10.3.

Fundamentally, properties and events are constructs of the metadata intended for use by tools that target the CLI and are not directly supported by the VES itself. Therefore, it is the job of the source language compiler and the reflection library (see Partition IV) to determine rules for name hiding, inheritance, and so forth. The source compiler shall generate CIL that directly accesses the methods named by the events and properties, not the events or properties themselves.

The specification says nothing about how a compiler should implement this. In the case of C# properties are implemented via separate getter and setter methods which can be declared virtual and override. Likewise, for events there are separate addhandler and removehandler methods.

So the simple answer is that properties and events are strictly metadata devoid of any implementation according to the specification. That is why they cannot be inherited in the same sense that methods are.

Brian Gideon
Aren
I have to admit that I thought it was pretty strange too. I am surprised this question didn't get more up votes.
Brian Gideon