When defining custom attributes, AttributeUsageAttribute must be used to indicate where the attribute can be applied. This will:
public sealed class MyAttribute : Attribute // Noncompliant - AttributeUsage is missing
{
private string text;
public MyAttribute(string text)
{
this.text = text;
}
public string Text => text;
}
[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;
}