Skip to content

Gendarme.Rules.Performance.AvoidUnsealedConcreteAttributesRule(git)

Sebastien Pouliot edited this page Mar 2, 2011 · 1 revision

AvoidUnsealedConcreteAttributesRule

Assembly: Gendarme.Rules.Performance
Version: git

Description

This rule fires if an attribute is defined which is both concrete (i.e. not abstract) and unsealed. This is a performance problem because it means that System.Attribute.GetCustomAttribute has to search the attribute type hierarchy for derived types. To fix this either seal the type or make it abstract.

Examples

Bad example:

[AttributeUsage (AttributeTargets.All)]
public class BadAttribute : Attribute {
}

Good example (sealed):

[AttributeUsage (AttributeTargets.All)]
public sealed class SealedAttribute : Attribute {
}

Good example (abstract and sealed):

[AttributeUsage (AttributeTargets.All)]
public abstract class AbstractAttribute : Attribute {
}
[AttributeUsage (AttributeTargets.All)]
public sealed class ConcreteAttribute : AbstractAttribute {
}

Notes

  • Before Gendarme 2.0 this rule was named AvoidUnsealedAttributesRule.

Source code

You can browse the latest source code of this rule on github.com

Clone this wiki locally