Mark attributes with AttributeUsageAttribute
Mark attributes with AttributeUsageAttribute
Microsoft docsDescription
When you define a custom attribute, mark it by using System.AttributeUsageAttribute to indicate where in the source code the custom attribute can be applied. The meaning and intended usage of an attribute will determine its valid locations in code. For example, you might define an attribute that identifies the person who is responsible for maintaining and enhancing each type in a library, and that responsibility is always assigned at the type level. In this case, compilers should enable the attribute on classes, enumerations, and interfaces, but should not enable it on methods, events, or properties. Organizational policies and procedures would dictate whether the attribute should be enabled on assemblies.
The System.AttributeTargets enumeration defines the targets that you can specify for a custom attribute. If you omit System.AttributeUsageAttribute, your custom attribute will be valid for all targets, as defined by the All value of System.AttributeTargets enumeration.
Cause
The System.AttributeUsageAttribute attribute is not present on the custom attribute.
How to fix violations
To fix a violation of this rule, specify targets for the attribute by using System.AttributeUsageAttribute. See the following example.
When to suppress
You should fix a violation of this rule instead of excluding the message. Even if the attribute inherits System.AttributeUsageAttribute, the attribute should be present to simplify code maintenance.