disable broken tests on net_4_0
[mcs.git] / docs / ecma334 / 24.2.xml
blobda3707e40c4b9d75d4435b5560a3d9dbe213df9f
1 <?xml version="1.0"?>
2 <clause number="24.2" title="Attribute specification">
3   <paragraph>Attribute specification is the application of a previously defined attribute to a declaration. An attribute is a piece of additional declarative information that is specified for a declaration. Attributes can be specified at global scope (to specify attributes on the containing assembly) and for <non_terminal where="16.5">type-declaration</non_terminal>s (<hyperlink>16.5</hyperlink>),  <non_terminal where="17.2">class-member-declaration</non_terminal>s (<hyperlink>17.2</hyperlink>), <non_terminal where="20.2">interface-member-declaration</non_terminal>s (<hyperlink>20.2</hyperlink>), <non_terminal where="21.3">enum-member-declaration</non_terminal>s (<hyperlink>21.1</hyperlink>), <non_terminal where="17.6.2">accessor-declarations</non_terminal> (<hyperlink>17.6.2</hyperlink>), <non_terminal where="17.7">event-accessor-declarations</non_terminal> (<hyperlink>17.7</hyperlink>), and <non_terminal where="17.5.1">formal-parameter-list</non_terminal>s (<hyperlink>17.5.1</hyperlink>). </paragraph>
4   <paragraph>Attributes are specified in attribute sections. An attribute section consists of a pair of square brackets, which surround a comma-separated list of one or more attributes. The order in which attributes are specified in such a list, and the order in which sections attached to the same program entity are arranged, is not significant. For instance, the attribute specifications [A][B], [B][A], [A, B], and [B, A] are equivalent. <grammar_production><name><non_terminal where="24.2">global-attributes</non_terminal></name> : <rhs><non_terminal where="24.2">global-attribute-sections</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">global-attribute-section</non_terminal>s</name> : <rhs><non_terminal where="24.2">global-attribute-section</non_terminal></rhs><rhs><non_terminal where="24.2">global-attribute-sections</non_terminal><non_terminal where="24.2">global-attribute-section</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">global-attribute-section</non_terminal></name> : <rhs><terminal>[</terminal><non_terminal where="24.2">global-attribute-target-specifier</non_terminal><non_terminal where="24.2">attribute-list</non_terminal><terminal>]</terminal></rhs><rhs><terminal>[</terminal><non_terminal where="24.2">global-attribute-target-specifier</non_terminal><non_terminal where="24.2">attribute-list</non_terminal><terminal>,]</terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">global-attribute-target-specifier</non_terminal></name> : <rhs><non_terminal where="24.2">global-attribute-target</non_terminal><terminal>:</terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">global-attribute-target</non_terminal></name> : <rhs><terminal>assembly  </terminal></rhs></grammar_production><grammar_production><name>attributes</name> : <rhs><non_terminal where="24.2">attribute-sections</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">attribute-section</non_terminal>s</name> : <rhs><non_terminal where="24.2">attribute-section</non_terminal></rhs><rhs><non_terminal where="24.2">attribute-sections</non_terminal><non_terminal where="24.2">attribute-section</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">attribute-section</non_terminal></name> : <rhs><terminal>[</terminal><non_terminal where="24.2">attribute-target-specifier</non_terminal><opt/><non_terminal where="24.2">attribute-list</non_terminal><terminal>]</terminal></rhs><rhs><terminal>[</terminal><non_terminal where="24.2">attribute-target-specifier</non_terminal><opt/><non_terminal where="24.2">attribute-list</non_terminal><terminal>,]</terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">attribute-target-specifier</non_terminal></name> : <rhs><non_terminal where="24.2">attribute-target</non_terminal><terminal>:</terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">attribute-target</non_terminal></name> : <rhs><terminal>field </terminal></rhs><rhs><terminal>event </terminal></rhs><rhs><terminal>method </terminal></rhs><rhs><terminal>param </terminal></rhs><rhs><terminal>property </terminal></rhs><rhs><terminal>return  </terminal></rhs><rhs><terminal>type  </terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">attribute-list</non_terminal></name> : <rhs><non_terminal where="24.2">attribute</non_terminal></rhs><rhs><non_terminal where="24.2">attribute-list</non_terminal><terminal>,</terminal><non_terminal where="24.2">attribute</non_terminal></rhs></grammar_production><grammar_production><name>attribute</name> : <rhs><non_terminal where="24.2">attribute-name</non_terminal><non_terminal where="24.2">attribute-arguments</non_terminal><opt/></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">attribute-name</non_terminal></name> : <rhs><non_terminal where="10.8">type-name</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">attribute-arguments</non_terminal></name> : <rhs><terminal>(</terminal><non_terminal where="24.2">positional-argument-list</non_terminal><opt/><terminal>)</terminal></rhs><rhs><terminal>(</terminal><non_terminal where="24.2">positional-argument-list</non_terminal><terminal>,</terminal><non_terminal where="24.2">named-argument-list</non_terminal><terminal>)</terminal></rhs><rhs><terminal>(</terminal><non_terminal where="24.2">named-argument-list</non_terminal><terminal>)</terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">positional-argument-list</non_terminal></name> : <rhs><non_terminal where="24.2">positional-argument</non_terminal></rhs><rhs><non_terminal where="24.2">positional-argument-list</non_terminal><terminal>,</terminal><non_terminal where="24.2">positional-argument</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">positional-argument</non_terminal></name> : <rhs><non_terminal where="24.2">attribute-argument-expression</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">named-argument-list</non_terminal></name> : <rhs><non_terminal where="24.2">named-argument</non_terminal></rhs><rhs><non_terminal where="24.2">named-argument-list</non_terminal><terminal>,</terminal><non_terminal where="24.2">named-argument</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">named-argument</non_terminal></name> : <rhs><non_terminal where="9.4.2">identifier</non_terminal><terminal>=</terminal><non_terminal where="24.2">attribute-argument-expression</non_terminal></rhs></grammar_production><grammar_production><name><non_terminal where="24.2">attribute-argument-expression</non_terminal></name> : <rhs><non_terminal where="14.14">expression</non_terminal></rhs></grammar_production></paragraph>
5   <paragraph>An attribute consists of an <non_terminal where="24.2">attribute-name</non_terminal> and an optional list of positional and named arguments. The positional arguments (if any) precede the named arguments. A positional argument consists of an  attribute-argument-expression; a named argument consists of a name, followed by an equal sign, followed by an <non_terminal where="24.2">attribute-argument-expression</non_terminal>, which, together, are constrained by the same rules as simple assignment.) The order of named arguments is not significant. </paragraph>
6   <paragraph>The <non_terminal where="24.2">attribute-name</non_terminal> identifies an attribute class. If the form of <non_terminal where="24.2">attribute-name</non_terminal> is <non_terminal where="10.8">type-name</non_terminal> then this name must refer to an attribute class. Otherwise, a compile-time error occurs. <example>[Example: The example <code_example><![CDATA[
7 class Class1 {}  
8 [Class1] class Class2 {}  // Error  
9 ]]></code_example>results in a compile-time error because it attempts to use Class1 as an attribute class when Class1 is not an attribute class. end example]</example> </paragraph>
10   <paragraph>Certain contexts permit the specification of an attribute on more than one target. A program can explicitly specify the target by including an <non_terminal where="24.2">attribute-target-specifier</non_terminal>. When an attribute is placed at the global level, a <non_terminal where="24.2">global-attribute-target-specifier</non_terminal> is required. In all other locations, a reasonable default is applied, but an <non_terminal where="24.2">attribute-target-specifier</non_terminal> can be used to affirm or override the default in certain ambiguous cases (or to just affirm the default in non-ambiguous cases). Thus, typically, <non_terminal where="24.2">attribute-target-specifier</non_terminal>s can be omitted except at the global level. The potentially ambiguous contexts are resolved as follows: <list><list_item> An attribute specified on a delegate declaration can apply either to the delegate being declared or to its return value. In the absence of an <non_terminal where="24.2">attribute-target-specifier</non_terminal>, the attribute applies to the delegate. The presence of the type <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the delegate; the presence of the return <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the return value. </list_item><list_item> An attribute specified on a method declaration can apply either to the method being declared or to its return value. In the absence of an <non_terminal where="24.2">attribute-target-specifier</non_terminal>, the attribute applies to the method. The presence of the method <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the method; the presence of the return <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the return value. </list_item><list_item> An attribute specified on an operator declaration can apply either to the operator being declared or to its return value of this declaration. In the absence of an <non_terminal where="24.2">attribute-target-specifier</non_terminal>, the attribute applies to the operator. The presence of the type <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the operator; the presence of the return <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the return value. </list_item><list_item> An attribute specified on an event declaration that omits event accessors can apply to the event being declared, to the associated field (if the event is not abstract), or to the associated add and remove methods. In the absence of an <non_terminal where="24.2">attribute-target-specifier</non_terminal>, the attribute applies to the event declaration. The presence of the event <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the event; the presence of the field <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the field; and the presence of the method <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the methods. </list_item><list_item> An attribute specified on a get accessor declaration for a property or indexer declaration can apply either to the associated method or to its return value. In the absence of an <non_terminal where="24.2">attribute-target-specifier</non_terminal>, the attribute applies to the method. The presence of the method <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the method; the presence of the return <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the return value. </list_item><list_item> An attribute specified on a set accessor for a property or indexer declaration can apply either to the associated method or to its lone implicit parameter. In the absence of an <non_terminal where="24.2">attribute-target-specifier</non_terminal>, the attribute applies to the method. The presence of the method <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the method; the presence of the param <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the parameter. </list_item><list_item> An attribute specified on an add or remove accessor declaration for an event declaration can apply either to the associated method or to its lone parameter. In the absence of an <non_terminal where="24.2">attribute-target-specifier</non_terminal>, the attribute applies to the method. The presence of the method <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the method; the presence of the param <non_terminal where="24.2">attribute-target-specifier</non_terminal> indicates that the attribute applies to the parameter. </list_item></list></paragraph>
11   <paragraph>An implementation may accept other attribute target specifiers, the purpose of which is  implementation-defined. However, an implementation that does not recognize such a target, shall issue a warning. </paragraph>
12   <paragraph>By convention, attribute classes are named with a suffix of Attribute. An <non_terminal where="24.2">attribute-name</non_terminal> of the form  <non_terminal where="10.8">type-name</non_terminal> may either include or omit this suffix. If an attribute class is found both with and without this suffix, an ambiguity is present, and a compile-time error shall be issued. If the <non_terminal where="24.2">attribute-name</non_terminal> is spelled using a verbatim identifier (<hyperlink>9.4.2</hyperlink>), then only an attribute without a suffix is matched, thus enabling such an ambiguity to be resolved. <example>[Example: The example <code_example><![CDATA[
13 using System;  
14 [AttributeUsage(AttributeTargets.All)]  
15 public class X: Attribute  
16 {}  
17 [AttributeUsage(AttributeTargets.All)]  
18 public class XAttribute: Attribute  
19 {}  
20 [X]      // error: ambiguity  
21 class Class1 {}  
22 [XAttribute]    // refers to XAttribute  
23 class Class2 {}  
24 [@X]          // refers to X  
25 class Class3 {}  
26 [@XAttribute]    // refers to XAttribute  
27 class Class4 {}  
28 ]]></code_example>shows two attribute classes named X and XAttribute. The attribute [X] is ambiguous, since it could refer to either X or XAttribute. Using a verbatim identifier allows the exact intent to be specified in such rare cases. The attribute [XAttribute] is not ambiguous (although it would be if there was an attribute class named XAttributeAttribute!). If the declaration for class X is removed, then both attributes refer to the attribute class named XAttribute, as follows: <code_example><![CDATA[
29 using System;  
30 [AttributeUsage(AttributeTargets.All)]  
31 public class XAttribute: Attribute  
32 {}  
33 [X]      // refers to XAttribute  
34 class Class1 {}  
35 [XAttribute]    // refers to XAttribute  
36 class Class2 {}  
37 [@X]      // error: no attribute named "X"  
38 class Class3 {}  
39 ]]></code_example>end example]</example> </paragraph>
40   <paragraph>It is a compile-time error to use a single-use attribute class more than once on the same entity. <example>[Example: The example <code_example><![CDATA[
41 using System;  
42 [AttributeUsage(AttributeTargets.Class)]  
43 public class HelpStringAttribute: Attribute  
44 {  
45    string value;  
46    public HelpStringAttribute(string value) {  
47       this.value = value;  
48    }  
49    public string Value { get {...} }  
50 }  
51 [HelpString("Description of Class1")]  
52 [HelpString("Another description of Class1")]  
53 public class Class1 {}  
54 ]]></code_example>results in a compile-time error because it attempts to use HelpString, which is a single-use attribute class, more than once on the declaration of Class1. end example]</example> </paragraph>
55   <paragraph>An expression E is an <non_terminal where="24.2">attribute-argument-expression</non_terminal> if all of the following statements are true: <list><list_item> The type of E is an attribute parameter type (<hyperlink>24.1.3</hyperlink>). </list_item><list_item> At compile-time, the value of E can be resolved to one of the following: </list_item><list><list_item> A constant value. </list_item><list_item> A System.Type object. </list_item><list_item> A one-dimensional array of <non_terminal where="24.2">attribute-argument-expression</non_terminal>s. </list_item></list></list></paragraph>
56   <paragraph>
57     <example>[Example: For example: <code_example><![CDATA[
58 using System;  
59 [AttributeUsage(AttributeTargets.Class)]  
60 public class MyAttribute: Attribute  
61 {  
62    public int P1 {  
63       get {...}  
64       set {...}  
65    }  
66    public Type P2 {  
67       get {...}  
68       set {...}  
69    }  
70    public object P3 {  
71       get {...}  
72       set {...}  
73    }  
74 }  
75 [My(P1 = 1234, P3 = new int[]{1, 3, 5}, P2 = typeof(float))]  
76 class MyClass {}  
77 ]]></code_example>end example]</example>
78   </paragraph>
79 </clause>