代码改变世界

[FxCop.设计规则]14. 不要在封闭类中定义保护型成员

  Colin Han  阅读(1871)  评论(0编辑  收藏  举报

14.     不要在封闭类中定义保护型成员

翻译概述:

使用Sealed声明密封类可以提高程序的执行效率(详细内容可以看:sealed能提高性能),因此比较推荐将不再需要继承的类型声明为密封类型。

当一个类型被声明为密封类型后,在其中定义保护型成员就变得没有意义了。同时,这样做可能会增加将来的维护成本(见本文中的例外段)。  

个人认为,密封类型并没有被编译器优化从而增加程序的效率(使用ILDASM检查并没有发现密封类和非密封类有什么不同),只是通过将类型声明为密封类型。可以给使用者传达一个明确的信息,使用者可以根据这些信息对代码进行一些优化,提高使用这个类型的效率。例如在FxCop的另外一条性能规则中,微软说他们对Attribute.GetCustomAttribute进行了一些优化,如果发现Attribute是一个封闭类型,会减少一些逻辑,从而提高效率。

引起的原因:

一个公共类型声明了一个保护型的成员。但是这个规则不检查析构函数,Which must follow this pattern 不明白)

描述:

使用保护型成员是为了确保只有这个类型的继承类型可以访问或者重写这些成员。但是,按照定义,封闭类将不能被继承。因此声明的保护型成员将不会被调用。

C#编译器会识别这个问题并产生一个编译时警告。

修复:

将这些成员修改成私有成员,或者修改这个类型为可继承的类型(非封闭类型)

例外:

不要忽略这个问题。否则,将为增加维护的成本,并且不会带来任何好处。

例程:

原文中给出了两个类型,分别使用C#VB实现,这两个类型都违背了这条规则。

译注:

正如在翻译概述中所说的,封闭类型会给使用者传达一个暗示,使用者会根据这些暗示对代码进行一些优化,因此将一个封闭类型修改为非封闭类型是一个比较危险的行为。同时,在密封类型中声明保护型成员也会使使用者难以理解设计者的意图(事实上,设计者可能自己也不知道自己的意图了。)

原文引用:

Do not declare protected members in sealed types

TypeName:

DoNotDeclareProtectedMembersInSealedTypes

CheckId:

CA1047

Category:

Microsoft.Design

Message Level:

Error

Certainty:

95%

Breaking Change:

NonBreaking


Cause: A public type is sealed (NotInheritable in Visual basic) and declares a protected member. This rule does not report violations for Finalize methods, which must follow this pattern.

Rule Description

Types declare protected members so that inheriting types can access or override the member. By definition, you cannot inherit from a sealed type, which means that protected methods on sealed types cannot be called.

The C# compiler issues a warning for this error.

How to Fix Violations

To fix a violation of this rule, change the access level of the member to private or make the type inheritable.

When to Exclude Messages

Do not exclude a message from this rule. Leaving the type in its current state can cause maintenance issues and does not provide any benefits.

Example Code

The following example shows a type that violates this rule.

[Visual Basic]

Imports System

Namespace DesignLibrary

   
Public NotInheritable Class BadSealedType
      
Protected  Sub MyMethod
      
End Sub

   
End Class


End Namespace


[C#]

using System;

namespace DesignLibrary
{
   
public sealed class SealedClass
   
{
      
protected void ProtectedMethod(){}
   }

}

编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· MQ 如何保证数据一致性?
点击右上角即可分享
微信分享提示