当前位置 : 主页 > 网络编程 > ASP >

asp.net-mvc – 为什么System.ComponentModel.DataAnnotations.DisplayAttribute已密封?

来源:互联网 收集:自由互联 发布时间:2021-06-24
我打算实现一个自定义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命名空间中的元素却没有.

网友评论