Attribute.IsDefined与MemberInfo.IsDefined-继承参数的差异 [英] Attribute.IsDefined vs MemberInfo.IsDefined - difference of the inherited parameter

查看:272
本文介绍了Attribute.IsDefined与MemberInfo.IsDefined-继承参数的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题涉及对属性继承属性的检查.

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):

如果为true,则指定还搜索元素的祖先以获取自定义属性.

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

(2):

true,以搜索此成员的继承链以找到属性; 属性和事件将忽略此参数;请参阅备注.

(此备注仅建议使用(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.IsDefinedEventInfo.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-继承参数的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆