问题描述
我的问题涉及对属性继承属性的检查.
My question deals with the check of inherited attributes of properties.
类似于此问题中所述的方法 Attribute.IsDefined(MemberInfo,Type,Boolean) (1)和 MemberInfo.IsDefined(Type,Boolean) (2)基本相同.两者都在检查是否使用了属性.但是,如果要检查属性,则继承参数会存在严重差异:
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):
(2):
(此备注仅建议使用(1))
使用(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.
我忽略了什么吗?
推荐答案
我的猜测(并且不再是的猜测)-原因是向后兼容.
My guess (and it is no more a guess) - the reason is backwards compatibility.
PropertyInfo.IsDefined
和EventInfo.IsDefined
忽略了inherit
参数,因为它们是在.NET 1.0中引入的. .c 2.0中引入了Attribute
上的静态方法,并对其进行了修改以支持属性和事件的继承.如果要更改这种行为,将inherit
设置为true的旧代码可能会开始获得意外的属性.
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.
只是BCL中许多不一致的问题之一...:)
Just one of many inconsistencies in the BCL... :)
这篇关于Attribute.IsDefined与MemberInfo.IsDefined-继承参数的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!