Avoid Unsealed Attributes

ID

csharp.avoid_unsealed_attributes

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Efficiency

Language

CSharp

Tags

attributes, efficiency, performance

Description

Reports custom Attribute classes declared without the sealed modifier. The CLR’s attribute-reflection paths are significantly faster when the attribute type is sealed because they can skip the inheritance walk.

Rationale

The .NET design guidelines explicitly recommend sealing attribute classes. Every attribute in the base class library is sealed (SerializableAttribute, ObsoleteAttribute, FlagsAttribute, …​). Leaving an attribute open for inheritance pays a reflection cost on every lookup and almost never reflects an intentional extensibility point.

public class MyAttribute : Attribute { }              // FLAW

public sealed class MyOtherAttribute : Attribute { }  // OK

Remediation

Add the sealed modifier to the class header. If the attribute is genuinely intended as a base for further attributes, mark it abstract instead and document the extensibility point.