Skip to content

Commit 706b31c

Browse files
committed
Add additional text
A couple other locations needed updates for generic attribute classes.
1 parent 9723396 commit 706b31c

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

standard/attributes.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ Attributes are defined through the declaration of attribute classes ([§23.2](at
1616

1717
A class that derives from the abstract class `System.Attribute`, whether directly or indirectly, is an ***attribute class***. The declaration of an attribute class defines a new kind of attribute that can be placed on program entities. By convention, attribute classes are named with a suffix of `Attribute`. Uses of an attribute may either include or omit this suffix.
1818

19-
A generic class declaration shall not use `System.Attribute` as a direct or indirect base class.
19+
An attribute class may be generic. When an *attribute_name* references a generic attribute class, each type argument is subject to the same restrictions that apply to a type argument of the `typeof` operator ([§12.8.18](expressions.md#12818-the-typeof-operator)); for example, `dynamic`, a tuple type with element names, a nullable reference type, `nint`, and `nuint` are not permitted as type arguments. (In addition, a type parameter of the surrounding declaration shall not be used as a type argument; see §8.5.)
2020

2121
> *Example*:
2222
>
23-
> <!-- Example: {template:"standalone-lib", name:"AttributeCantBeGeneric", expectedErrors:["CS8936"], ignoredWarnings:["CS0169"]} -->
23+
> <!-- Example: {template:"standalone-lib", name:"GenericAttribute", ignoredWarnings:["CS0169"]} -->
2424
> ```csharp
25-
> public class B : Attribute {}
26-
> public class C<T> : B {} // Error – generic cannot be an attribute
25+
> using System;
26+
> public class Attr<T> : Attribute { } // OK – generic attribute class
27+
>
28+
> [Attr<int>] // OK
29+
> public class C1 { }
2730
> ```
2831
>
2932
> *end example*
@@ -61,6 +64,8 @@ The attribute `AttributeUsage` ([§23.5.2](attributes.md#2352-the-attributeusage
6164
6265
`AttributeUsage` has a named parameter ([§23.2.3](attributes.md#2323-positional-and-named-parameters)), called `AllowMultiple`, which indicates whether the attribute can be specified more than once for a given entity. If `AllowMultiple` for an attribute class is true, then that attribute class is a ***multi-use attribute class***, and can be specified more than once on an entity. If `AllowMultiple` for an attribute class is false or it is unspecified, then that attribute class is a ***single-use attribute class***, and can be specified at most once on an entity.
6366
67+
> *Note*: For a generic attribute class, multiplicity is determined by the unbound generic attribute class definition, not by individual closed constructions. Therefore, when `AllowMultiple` is false or unspecified, two attribute applications that differ only in their type arguments shall not both appear on the same entity. *end note*
68+
6469
> *Example*: The following example defines a multi-use attribute class named `AuthorAttribute` and shows a class declaration with two uses of the `Author` attribute:
6570
>
6671
> <!-- Example: {template:"standalone-lib", name:"AttributeUsage4", replaceEllipsis:true} -->

0 commit comments

Comments
 (0)