Fruchtzwerg
Fruchtzwerg

Reputation: 11399

Attribute.IsDefined vs MemberInfo.IsDefined - difference of the inherited parameter

My question deals with the check of inherited attributes of properties.

Like described in this Question the methods Attribute.IsDefined(MemberInfo, Type, Boolean) (1) and MemberInfo.IsDefined(Type, Boolean) (2) are basically the same. Both are checking if an attribute is used. But there is a serious difference at the inherit parameter if I want to check a property:

(1):

If true, specifies to also search the ancestors of element for custom attributes.

(2):

true to search this member's inheritance chain to find the attributes; otherwise, false.This parameter is ignored for properties and events; see Remarks.

(The remarks only recommend the usage of (1))

It´s no problem to use (1) to get a inherited attribute of a property. But my question is the why: Why is this feature implemented like this? What is the reason, the usage or the benefit? From my point, there is a method who ignores a parameter and thats not really a clean solution.

Did I overlook something?

Upvotes: 2

Views: 770

Answers (1)

Eli Arbel
Eli Arbel

Reputation: 22749

My guess (and it is no more a guess) - the reason is backwards compatibility.

PropertyInfo.IsDefined and EventInfo.IsDefined ignored the inherit parameter since they were introduced in .NET 1.0. The static methods on Attribute were introduced in .NET 2.0, and were amended to support inheritance of properties and events. If they were to change that behavior, old code that has set inherit to true could start getting unexpected attributes.

Just one of many inconsistencies in the BCL... :)

Upvotes: 5

Related Questions