CA1501:避免過度繼承
型別名稱 |
AvoidExcessiveInheritance |
CheckId |
CA1501 |
分類 |
Microsoft.Maintainability |
中斷變更 |
中斷 |
原因
型別在其繼承階層架構 (Inheritance Hierarchy) 中超過四個層級的深度。
規則描述
太深的巢狀型別階層架構可能會難以依循、了解和維護。這項規則會使階層架構的分析限制在相同的模組內。
如何修正違規
若要修正此規則的違規情形,請從繼承階層架構中較淺的基底型別 (Base Type) 衍生型別,或排除某些中繼基底型別。
隱藏警告的時機
您可以放心地隱藏這項規則的警告。不過,程式碼可能會更難維護。請注意,視基底型別的可視性而定,解決這項規則的違規情形可能會產生中斷變更。例如,移除公用基底型別為中斷變更。
範例
下列範例顯示違反規則的型別。
Imports System
Namespace MaintainabilityLibrary
Class BaseClass
End Class
Class FirstDerivedClass
Inherits BaseClass
End Class
Class SecondDerivedClass
Inherits FirstDerivedClass
End Class
Class ThirdDerivedClass
Inherits SecondDerivedClass
End Class
Class FourthDerivedClass
Inherits ThirdDerivedClass
End Class
' This class violates the rule.
Class FifthDerivedClass
Inherits FourthDerivedClass
End Class
End Namespace
using System;
namespace MaintainabilityLibrary
{
class BaseClass {}
class FirstDerivedClass : BaseClass {}
class SecondDerivedClass : FirstDerivedClass {}
class ThirdDerivedClass : SecondDerivedClass {}
class FourthDerivedClass : ThirdDerivedClass {}
// This class violates the rule.
class FifthDerivedClass : FourthDerivedClass {}
}