-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
593fc71
commit 6ffc193
Showing
2 changed files
with
37 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,48 @@ | ||
<h2>Why is this an issue?</h2> | ||
<p>When defining custom attributes, <code>System.AttributeUsageAttribute</code> must be used to indicate where the attribute can be applied. This will | ||
determine its valid locations in the code.</p> | ||
<h3>Noncompliant code example</h3> | ||
<pre> | ||
using System; | ||
|
||
namespace MyLibrary | ||
<p>When defining custom attributes, <a href="https://learn.microsoft.com/en-us/dotnet/api/system.attributeusageattribute">AttributeUsageAttribute</a> | ||
must be used to indicate where the attribute can be applied. This will:</p> | ||
<ul> | ||
<li> indicate how the attribute can be used </li> | ||
<li> prevent it from being used at invalid locations </li> | ||
</ul> | ||
<h2>How to fix it</h2> | ||
<h3>Code examples</h3> | ||
<h4>Noncompliant code example</h4> | ||
<pre data-diff-id="1" data-diff-type="noncompliant"> | ||
public sealed class MyAttribute : Attribute // Noncompliant - AttributeUsage is missing | ||
{ | ||
private string text; | ||
|
||
public sealed class MyAttribute :Attribute // Noncompliant | ||
{ | ||
string text; | ||
public MyAttribute(string text) | ||
{ | ||
this.text = text; | ||
} | ||
|
||
public MyAttribute(string myText) | ||
{ | ||
text = myText; | ||
} | ||
public string Text | ||
{ | ||
get | ||
{ | ||
return text; | ||
} | ||
} | ||
} | ||
public string Text => text; | ||
} | ||
</pre> | ||
<h3>Compliant solution</h3> | ||
<pre> | ||
using System; | ||
|
||
namespace MyLibrary | ||
<h4>Compliant solution</h4> | ||
<pre data-diff-id="1" data-diff-type="compliant"> | ||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)] | ||
public sealed class MyAttribute : Attribute | ||
{ | ||
private string text; | ||
|
||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)] | ||
public sealed class MyAttribute :Attribute | ||
{ | ||
string text; | ||
public MyAttribute(string text) | ||
{ | ||
this.text = text; | ||
} | ||
|
||
public MyAttribute(string myText) | ||
{ | ||
text = myText; | ||
} | ||
public string Text | ||
{ | ||
get | ||
{ | ||
return text; | ||
} | ||
} | ||
} | ||
public string Text => text; | ||
} | ||
</pre> | ||
<h2>Resources</h2> | ||
<h3>Documentation</h3> | ||
<ul> | ||
<li> Microsoft Learn - <a | ||
href="https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/reflection-and-attributes/creating-custom-attributes">Create custom | ||
attributes</a> </li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.attributeusageattribute">AttributeUsageAttribute class</a> </li> | ||
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.attribute">Attribute class</a> </li> | ||
</ul> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -227,6 +227,7 @@ | |
"S3973", | ||
"S3981", | ||
"S3984", | ||
"S3993", | ||
"S3998", | ||
"S4015", | ||
"S4019", | ||
|