Constructors of abstract classes should be declared as protected or private

Rule ND1806 Visibility

Constructors of abstract classes can only be called from derived classes, and instances of an abstract class cannot be created: a public constructor there is wrong design. A private constructor means derived classes must be nested in the abstract class.

How to fix

Declare the constructor protected, or don't declare the type abstract.

CQLinq source

This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.

//<Name>Constructors of abstract classes should be declared as protected or private</Name>
// <Id>ND1806:ConstructorsOfAbstractClassesShouldBeDeclaredAsProtectedOrPrivate</Id>

warnif count > 0
from t in Application.Types where 
  t.IsClass && 
  t.IsAbstract
let ctors = t.Constructors.Where(c => !c.IsProtected && 
                                      !c.IsPrivate &&
                                      !c.IsProtectedAndInternal)
where ctors.Count() > 0
from ctor in ctors
select new {
   ctor, 
   baseClass = t,
   Debt = 30.ToSeconds().ToDebt(),
   Severity = Severity.Medium
}

//<Expl>
// This constructor should be declared as *protected*
// since its parent *{1}* is abstract and can only be 
// instantiated from its derived classes.
//</Expl>

//<Desc>
// Constructors of abstract classes can only be called from derived classes,
// and instances of an *abstract* class cannot be created: a public
// constructor there is wrong design. A private constructor means derived
// classes must be nested in the abstract class.
//</Desc>

//<HowToFix>
// Declare the constructor *protected*, or don't declare the type *abstract*.
//</HowToFix>

Visibility: all 12 items