org.sonar.plugins.csharp.S3993.html Maven / Gradle / Ivy
Why is this an issue?
When defining custom attributes, AttributeUsageAttribute
must be used to indicate where the attribute can be applied. This will:
- indicate how the attribute can be used
- prevent it from being used at invalid locations
How to fix it
Code examples
Noncompliant code example
public sealed class MyAttribute : Attribute // Noncompliant - AttributeUsage is missing
{
private string text;
public MyAttribute(string text)
{
this.text = text;
}
public string Text => text;
}
Compliant solution
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)]
public sealed class MyAttribute : Attribute
{
private string text;
public MyAttribute(string text)
{
this.text = text;
}
public string Text => text;
}
Resources
Documentation
- Microsoft Learn - Create custom
attributes
- Microsoft Learn - AttributeUsageAttribute class
- Microsoft Learn - Attribute class
© 2015 - 2024 Weber Informatics LLC | Privacy Policy