Attribute classes should inherit directly from System.Attribute and should therefore be protected from further inheritance.
Attribute-based programming is a powerful programming paradigm that is inherently available in some of today's newer programming languages, .NET included. You can integrate the power and flexibility of attributes into your own code by creating and using your own custom attributes. When doing this you should ensure that custom attribute classes are inherited directly from System.Attribute. It is required that all Attribute classes inherit (directly or indirectly) from System.Attribute it is recommended that you only inherit directly from System.Attribute for both performance and avoidance of design issues.
This rule violation can be automatically corrected with devAdvantage
Example of custom Attribute class not being sealed (incorrect code)
[C#]
public class MyAttributeBase : System.Attribute
{
// base implementation
}
public class DeprecatedAttribute : MyAttributeBase
{
private string replacementFeature = "";
public DeprecatedAttribute( string ReplacementFeatureName )
{
replacementFeature = ReplacementFeatureName;
}
public override string ToString()
{
if ( replacementFeature.Length == 0 )
return "Feature is Deprecated";
else
return "Feature is Deprecated, please use " + replacementFeature;
}
public string ReplacementFeature
{
get { return replacementFeature; }
}
}
Example of sealed attribute class (correct code)
[C#]
public sealed class DeprecatedAttribute : System.Attribute
{
private string replacementFeature = "";
public DeprecatedAttribute( string ReplacementFeatureName )
{
replacementFeature = ReplacementFeatureName;
}
public override string ToString()
{
if ( replacementFeature.Length == 0 )
return "Feature is Deprecated";
else
return "Feature is Deprecated, please use " + replacementFeature;
}
public string ReplacementFeature
{
get { return replacementFeature; }
}
}