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

asp.net-mvc – 为什么System.ComponentModel.DataAnnotations.D

发布时间:2020-12-16 09:23:59 所属栏目:asp.Net 来源:网络整理
导读:我打算实现一个自定义DisplayAttribute,以允许基于模型值的动态显示值,但我不能,因为DisplayAttribute是密封的. 在我开始编写我自己的客户属性来模拟DisplayAttribute的行为之前,有人能想到为什么这是密封的吗?我假设它背后有一个原因,如果是这样,那可能是
我打算实现一个自定义DisplayAttribute,以允许基于模型值的动态显示值,但我不能,因为DisplayAttribute是密封的.

在我开始编写我自己的客户属性来模拟DisplayAttribute的行为之前,有人能想到为什么这是密封的吗?我假设它背后有一个原因,如果是这样,那可能是我不应该试图通过滚动我自己“破解”这个限制的原因.

我不是要求任何人阅读微软的想法,我只是希望有人已经知道密封的设计理由,以便在滚动(或避免)我自己的实现时考虑到这一点.

解决方法

通常,密封属性被认为是最佳实践. FxCop有一个关于它的规则,defined here.从那个页面:

The .NET Framework class library provides methods for retrieving custom attributes. By default,these methods search the attribute inheritance hierarchy; for example Attribute.GetCustomAttribute searches for the specified attribute type,or any attribute type that extends the specified attribute type. Sealing the attribute eliminates the search through the inheritance hierarchy,and can improve performance.

许多MVC属性(ActionFilter等)都是未密封的,因为它们是专门为扩展而设计的,但DataAnnotations命名空间中的元素却没有.

(编辑:李大同)

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

    推荐文章
      热点阅读