加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – ICustomAttributeProvider.GetCustomAttributes(…):设计

发布时间:2020-12-15 21:08:11 所属栏目:百科 来源:网络整理
导读:MSDN文档: MemberInfo.GetCustomAttibutes Method (Type,Boolean)在备注中说明: This method ignores the inherit parameter for properties and events. To search the inheritance chain for attributes on properties and events,use the appropriate o
MSDN文档: MemberInfo.GetCustomAttibutes Method (Type,Boolean)在备注中说明:

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.

这基本上意味着事件成员和属性成员将忽略此实现的第二个参数(bool inherit).但是,调用Attribute.GetCustomAttributes(MemberInfo,Type,bool)这种操作的种类并没有.

令我困惑的是这个的设计.

为什么他们似乎在两种形式的成员类型上任意忽略继承属性?

如果有人能够对此有所了解,我将非常感激.

解决方法

这绝对令人困惑.严格的答案是属性和事件不会被继承,因此继承的参数没有意义.参考文献是 ECMA 335 CLI Specification第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.

规范没有说明编译器应该如何实现它.在C#的情况下,属性是通过单独的getter和setter方法实现的,这些方法可以声明为虚拟和覆盖.同样,对于事件,有单独的addhandler和removehandler方法.

所以简单的答案是属性和事件是严格的元数据,没有根据规范的任何实现.这就是为什么它们不能以与方法相同的意义继承.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读