c# - 为什么 .Net 最佳实践是将自定义属性设计为密封的?

标签 c# .net attributes

我正在阅读 Andrew Troelsen 撰写的 Pro C# 2010 和 .Net 4 平台

在关于属性的第 15 章中有一个注释:

Note: For security reasons, it is considered a .Net best practice to design all custom attributes as sealed.

作者没有解释为什么,有人能解释一下吗?

最佳答案

CA1813: Avoid unsealed attributes: 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.

引用:https://learn.microsoft.com/visualstudio/code-quality/ca1813-avoid-unsealed-attributes

属性只是在运行时发现的元数据。正如所引用的那样,如果其他人派生自您的自定义属性类,默认情况下 .NET 也会找到它们,如果派生的属性类以您意想不到的方式修改原始属性的行为,这可能意味着安全风险到。

尽管性能是密封属性类的主要原因,但这里有一个 formidable article dealing with its security side .

关于c# - 为什么 .Net 最佳实践是将自定义属性设计为密封的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7868218/

相关文章:

Javascript GIS - 循环feature.attributes

c# - MVC 5下拉列表用于编辑 View 中的多选值绑定(bind)

c# - RestRequest 类在哪里?

c# - 将秒表与异步方法一起使用

xslt - 我想使用 xslt 在 xml 文件中添加 "xmlns"属性

Javascript 更新属性

c# - Caliburn Micro Conductor + TransitioningContentControl

c# - 未知类型的动态转换以进行序列化

c# - 找不到预启动任务 'build'

c# - 如何确定计算机是否连接到 novell eDirectory 或 Microsoft ActiveDirectory?