rspec/rules/S3993/rule.adoc

64 lines
1.0 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-01-27 13:42:22 +01:00
When defining custom attributes, ``++System.AttributeUsageAttribute++`` must be used to indicate where the attribute can be applied. This will determine its valid locations in the code.
2020-06-30 12:48:39 +02:00
=== Noncompliant code example
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:39 +02:00
----
using System;
namespace MyLibrary
{
public sealed class MyAttribute :Attribute // Noncompliant
{
string text;
public MyAttribute(string myText)
{
text = myText;
}
public string Text
{
get
{
return text;
}
}
}
}
----
=== Compliant solution
2020-06-30 12:48:39 +02:00
2022-02-04 17:28:24 +01:00
[source,text]
2020-06-30 12:48:39 +02:00
----
using System;
namespace MyLibrary
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)]
public sealed class MyAttribute :Attribute
{
string text;
public MyAttribute(string myText)
{
text = myText;
}
public string Text
{
get
{
return text;
}
}
}
}
----