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

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

翻译概述:

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

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

 

 

个人认为,密封类型并没有被编译器优化从而增加程序的效率(使用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]

None.gif Imports  System
None.gif
ExpandedBlockStart.gifContractedBlock.gif
Namespace DesignLibrary Namespace DesignLibrary
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif   
Public NotInheritable Class BadSealedTypeClass BadSealedType
ExpandedSubBlockStart.gifContractedSubBlock.gif      
Protected  Sub MyMethod()Sub MyMethod
ExpandedSubBlockEnd.gif      
End Sub

ExpandedSubBlockEnd.gif   
End Class

InBlock.gif
ExpandedBlockEnd.gif
End Namespace

None.gif


[C#]

None.gif using  System;
None.gif
None.gif
namespace  DesignLibrary
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public sealed class SealedClass
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
protected void ProtectedMethod()dot.gif{}
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值