📄 24.2.htm
字号:
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Hyperlinked ECMA C# Language Specification</title><meta name="author" content="Jon Jagger" /><link rel="stylesheet" href="ecma334.css"></link></head><body><div align="right"><em><a href="http://www.jaggersoft.com">Jon Jagger</a></em></div><div align="right"><a href="mailto:jon@jaggersoft.com">jon@jaggersoft.com</a></div><form method="get" action="http://search.atomz.com/search/"><input size="30" name="sp-q"></input><input type="submit" value="Search C# Spec"></input><input type="hidden" name="sp-a" value="sp10024177"></input><input type="hidden" name="sp-f" value="ISO-8859-1"></input></form><a href="toc.htm">Table of Contents</a> <a href="1.htm">1</a> <a href="2.htm">2</a> <a href="3.htm">3</a> <a href="4.htm">4</a> <a href="5.htm">5</a> <a href="6.htm">6</a> <a href="7.htm">7</a> <a href="8.htm">8</a> <a href="9.htm">9</a> <a href="10.htm">10</a> <a href="11.htm">11</a> <a href="12.htm">12</a> <a href="13.htm">13</a> <a href="14.htm">14</a> <a href="15.htm">15</a> <a href="16.htm">16</a> <a href="17.htm">17</a> <a href="18.htm">18</a> <a href="19.htm">19</a> <a href="20.htm">20</a> <a href="21.htm">21</a> <a href="22.htm">22</a> <a href="23.htm">23</a> <a href="24.htm">24</a> <a href="25.htm">25</a> <a href="notes.htm">Notes</a> <a href="HyperlinkedCSharpECMA.zip">Download</a><span class="ruler"></span><span class="heading">ECMA-334 C# Language Specification</span><span class="navigate"><a href="24.1.3.htm"><img src="previous.gif" alt="previous" border="0" /></a><a href="24.3.htm"><img src="next.gif" alt="next" border="0" /></a></span><span class="clause-depth"><a href="23.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="25.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number-link"><a href="24.htm">24</a></span><span class="clause-title-previous"> Attributes</span></span><span class="clause-depth"><a href="24.1.htm"><img src="previous.gif" alt="previous at this level" border="0" /></a><a href="24.3.htm"><img src="next.gif" alt="next at this level" border="0" /></a> <span class="clause-number">24.2</span><span class="clause-title"> Attribute specification</span></span><span class="locator">
Paragraph 1</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P1S1"></a>Attribute specification is the application of a previously defined attribute to a declaration.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P1S2"></a>An attribute is a piece of additional declarative information that is specified for a declaration.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P1S3"></a>Attributes can be specified at global scope (to specify attributes on the containing assembly) and for <span class="non-terminal"><a href="16.5.htm#type-declaration">type-declaration</a></span>s (<a href="16.5.htm">§16.5</a>), <span class="non-terminal"><a href="17.2.htm#class-member-declaration">class-member-declaration</a></span>s (<a href="17.2.htm">§17.2</a>), <span class="non-terminal"><a href="20.2.htm#interface-member-declaration">interface-member-declaration</a></span>s (<a href="20.2.htm">§20.2</a>), <span class="non-terminal"><a href="21.3.htm#enum-member-declaration">enum-member-declaration</a></span>s (<a href="21.1.htm">§21.1</a>), <span class="non-terminal"><a href="17.6.2.htm#accessor-declarations">accessor-declarations</a></span> (<a href="17.6.2.htm">§17.6.2</a>), <span class="non-terminal"><a href="17.7.htm#event-accessor-declarations">event-accessor-declarations</a></span> (<a href="17.7.htm">§17.7</a>), and <span class="non-terminal"><a href="17.5.1.htm#formal-parameter-list">formal-parameter-list</a></span>s (<a href="17.5.1.htm">§17.5.1</a>).</span> </span><span class="locator">
Paragraph 2</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P2S1"></a>Attributes are specified in attribute sections.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P2S2"></a>An attribute section consists of a pair of square brackets, which surround a comma-separated list of one or more attributes.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P2S3"></a>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.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P2S4"></a>For instance, the attribute specifications [A][B], [B][A], [A, B], and [B, A] are equivalent.</span> <span class="grammar-production"><span class="name"><a name="global-attributes"></a>global-attributes</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#global-attribute-sections">global-attribute-sections</a></span> </span></span><span class="grammar-production"><span class="name"><a name="global-attribute-sections"></a>global-attribute-sections</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#global-attribute-section">global-attribute-section</a></span> </span><span class="rhs"><span class="non-terminal"><a href="24.2.htm#global-attribute-sections">global-attribute-sections</a></span> <span class="non-terminal"><a href="24.2.htm#global-attribute-section">global-attribute-section</a></span> </span></span><span class="grammar-production"><span class="name"><a name="global-attribute-section"></a>global-attribute-section</span> : <span class="rhs"><span class="terminal">[</span> <span class="non-terminal"><a href="24.2.htm#global-attribute-target-specifier">global-attribute-target-specifier</a></span> <span class="non-terminal"><a href="24.2.htm#attribute-list">attribute-list</a></span> <span class="terminal">]</span> </span><span class="rhs"><span class="terminal">[</span> <span class="non-terminal"><a href="24.2.htm#global-attribute-target-specifier">global-attribute-target-specifier</a></span> <span class="non-terminal"><a href="24.2.htm#attribute-list">attribute-list</a></span> <span class="terminal">,]</span> </span></span><span class="grammar-production"><span class="name"><a name="global-attribute-target-specifier"></a>global-attribute-target-specifier</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#global-attribute-target">global-attribute-target</a></span> <span class="terminal">:</span> </span></span><span class="grammar-production"><span class="name"><a name="global-attribute-target"></a>global-attribute-target</span> : <span class="rhs"><span class="terminal">assembly </span></span></span><span class="grammar-production"><span class="name"><a name="attributes"></a>attributes</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#attribute-sections">attribute-sections</a></span> </span></span><span class="grammar-production"><span class="name"><a name="attribute-sections"></a>attribute-sections</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#attribute-section">attribute-section</a></span> </span><span class="rhs"><span class="non-terminal"><a href="24.2.htm#attribute-sections">attribute-sections</a></span> <span class="non-terminal"><a href="24.2.htm#attribute-section">attribute-section</a></span> </span></span><span class="grammar-production"><span class="name"><a name="attribute-section"></a>attribute-section</span> : <span class="rhs"><span class="terminal">[</span> <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span><sub>opt</sub> <span class="non-terminal"><a href="24.2.htm#attribute-list">attribute-list</a></span> <span class="terminal">]</span> </span><span class="rhs"><span class="terminal">[</span> <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span><sub>opt</sub> <span class="non-terminal"><a href="24.2.htm#attribute-list">attribute-list</a></span> <span class="terminal">,]</span> </span></span><span class="grammar-production"><span class="name"><a name="attribute-target-specifier"></a>attribute-target-specifier</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#attribute-target">attribute-target</a></span> <span class="terminal">:</span> </span></span><span class="grammar-production"><span class="name"><a name="attribute-target"></a>attribute-target</span> : <span class="rhs"><span class="terminal">field </span></span><span class="rhs"><span class="terminal">event </span></span><span class="rhs"><span class="terminal">method </span></span><span class="rhs"><span class="terminal">param </span></span><span class="rhs"><span class="terminal">property </span></span><span class="rhs"><span class="terminal">return </span></span><span class="rhs"><span class="terminal">type </span></span></span><span class="grammar-production"><span class="name"><a name="attribute-list"></a>attribute-list</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#attribute">attribute</a></span> </span><span class="rhs"><span class="non-terminal"><a href="24.2.htm#attribute-list">attribute-list</a></span> <span class="terminal">,</span> <span class="non-terminal"><a href="24.2.htm#attribute">attribute</a></span> </span></span><span class="grammar-production"><span class="name"><a name="attribute"></a>attribute</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#attribute-name">attribute-name</a></span> <span class="non-terminal"><a href="24.2.htm#attribute-arguments">attribute-arguments</a></span><sub>opt</sub> </span></span><span class="grammar-production"><span class="name"><a name="attribute-name"></a>attribute-name</span> : <span class="rhs"><span class="non-terminal"><a href="10.8.htm#type-name">type-name</a></span> </span></span><span class="grammar-production"><span class="name"><a name="attribute-arguments"></a>attribute-arguments</span> : <span class="rhs"><span class="terminal">(</span> <span class="non-terminal"><a href="24.2.htm#positional-argument-list">positional-argument-list</a></span><sub>opt</sub> <span class="terminal">)</span> </span><span class="rhs"><span class="terminal">(</span> <span class="non-terminal"><a href="24.2.htm#positional-argument-list">positional-argument-list</a></span> <span class="terminal">,</span> <span class="non-terminal"><a href="24.2.htm#named-argument-list">named-argument-list</a></span> <span class="terminal">)</span> </span><span class="rhs"><span class="terminal">(</span> <span class="non-terminal"><a href="24.2.htm#named-argument-list">named-argument-list</a></span> <span class="terminal">)</span> </span></span><span class="grammar-production"><span class="name"><a name="positional-argument-list"></a>positional-argument-list</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#positional-argument">positional-argument</a></span> </span><span class="rhs"><span class="non-terminal"><a href="24.2.htm#positional-argument-list">positional-argument-list</a></span> <span class="terminal">,</span> <span class="non-terminal"><a href="24.2.htm#positional-argument">positional-argument</a></span> </span></span><span class="grammar-production"><span class="name"><a name="positional-argument"></a>positional-argument</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#attribute-argument-expression">attribute-argument-expression</a></span> </span></span><span class="grammar-production"><span class="name"><a name="named-argument-list"></a>named-argument-list</span> : <span class="rhs"><span class="non-terminal"><a href="24.2.htm#named-argument">named-argument</a></span> </span><span class="rhs"><span class="non-terminal"><a href="24.2.htm#named-argument-list">named-argument-list</a></span> <span class="terminal">,</span> <span class="non-terminal"><a href="24.2.htm#named-argument">named-argument</a></span> </span></span><span class="grammar-production"><span class="name"><a name="named-argument"></a>named-argument</span> : <span class="rhs"><span class="non-terminal"><a href="9.4.2.htm#identifier">identifier</a></span> <span class="terminal">=</span> <span class="non-terminal"><a href="24.2.htm#attribute-argument-expression">attribute-argument-expression</a></span> </span></span><span class="grammar-production"><span class="name"><a name="attribute-argument-expression"></a>attribute-argument-expression</span> : <span class="rhs"><span class="non-terminal"><a href="14.14.htm#expression">expression</a></span> </span></span></span><span class="locator">
Paragraph 3</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P3S1"></a>An attribute consists of an <span class="non-terminal"><a href="24.2.htm#attribute-name">attribute-name</a></span> and an optional list of positional and named arguments.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P3S2"></a>The positional arguments (if any) precede the named arguments.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P3S3"></a>A positional argument consists of an attribute-argument-expression; a named argument consists of a name, followed by an equal sign, followed by an <span class="non-terminal"><a href="24.2.htm#attribute-argument-expression">attribute-argument-expression</a></span>, which, together, are constrained by the same rules as simple assignment.)</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P3S4"></a>The order of named arguments is not significant.</span> </span><span class="locator">
Paragraph 4</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P4S1"></a>The <span class="non-terminal"><a href="24.2.htm#attribute-name">attribute-name</a></span> identifies an attribute class.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P4S2"></a>If the form of <span class="non-terminal"><a href="24.2.htm#attribute-name">attribute-name</a></span> is <span class="non-terminal"><a href="10.8.htm#type-name">type-name</a></span> then this name must refer to an attribute class.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P4S3"></a>Otherwise, a compile-time error occurs.</span> <span class="example">[Example: The example <pre class="code-example">
class Class1 {}
[Class1] class Class2 {} // Error
</pre>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]</span> </span><span class="locator">
Paragraph 5</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P5S1"></a>Certain contexts permit the specification of an attribute on more than one target.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P5S2"></a>A program can explicitly specify the target by including an <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span>.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P5S3"></a>When an attribute is placed at the global level, a <span class="non-terminal"><a href="24.2.htm#global-attribute-target-specifier">global-attribute-target-specifier</a></span> is required.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P5S4"></a>In all other locations, a reasonable default is applied, but an <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> can be used to affirm or override the default in certain ambiguous cases (or to just affirm the default in non-ambiguous cases).</span> <span class="sentence"><span class="sentence-number">5</span> <a name="P5S5"></a>Thus, typically, <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span>s can be omitted except at the global level.</span> <span class="sentence"><span class="sentence-number">6</span> <a name="P5S6"></a>The potentially ambiguous contexts are resolved as follows: </span><ul><li><span class="sentence"><span class="sentence-number">7</span> <a name="P5S7"></a> An attribute specified on a delegate declaration can apply either to the delegate being declared or to its return value.</span> <span class="sentence"><span class="sentence-number">8</span> <a name="P5S8"></a>In the absence of an <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span>, the attribute applies to the delegate.</span> <span class="sentence"><span class="sentence-number">9</span> <a name="P5S9"></a>The presence of the type <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the delegate; the presence of the return <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the return value.</span> </li><li><span class="sentence"><span class="sentence-number">10</span> <a name="P5S10"></a> An attribute specified on a method declaration can apply either to the method being declared or to its return value.</span> <span class="sentence"><span class="sentence-number">11</span> <a name="P5S11"></a>In the absence of an <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span>, the attribute applies to the method.</span> <span class="sentence"><span class="sentence-number">12</span> <a name="P5S12"></a>The presence of the method <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the method; the presence of the return <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the return value.</span> </li><li><span class="sentence"><span class="sentence-number">13</span> <a name="P5S13"></a> An attribute specified on an operator declaration can apply either to the operator being declared or to its return value of this declaration.</span> <span class="sentence"><span class="sentence-number">14</span> <a name="P5S14"></a>In the absence of an <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span>, the attribute applies to the operator.</span> <span class="sentence"><span class="sentence-number">15</span> <a name="P5S15"></a>The presence of the type <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the operator; the presence of the return <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the return value.</span> </li><li><span class="sentence"><span class="sentence-number">16</span> <a name="P5S16"></a> 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.</span> <span class="sentence"><span class="sentence-number">17</span> <a name="P5S17"></a>In the absence of an <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span>, the attribute applies to the event declaration.</span> <span class="sentence"><span class="sentence-number">18</span> <a name="P5S18"></a>The presence of the event <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the event; the presence of the field <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the field; and the presence of the method <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the methods.</span> </li><li><span class="sentence"><span class="sentence-number">19</span> <a name="P5S19"></a> 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.</span> <span class="sentence"><span class="sentence-number">20</span> <a name="P5S20"></a>In the absence of an <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span>, the attribute applies to the method.</span> <span class="sentence"><span class="sentence-number">21</span> <a name="P5S21"></a>The presence of the method <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the method; the presence of the return <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the return value.</span> </li><li><span class="sentence"><span class="sentence-number">22</span> <a name="P5S22"></a> 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.</span> <span class="sentence"><span class="sentence-number">23</span> <a name="P5S23"></a>In the absence of an <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span>, the attribute applies to the method.</span> <span class="sentence"><span class="sentence-number">24</span> <a name="P5S24"></a>The presence of the method <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the method; the presence of the param <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the parameter.</span> </li><li><span class="sentence"><span class="sentence-number">25</span> <a name="P5S25"></a> 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.</span> <span class="sentence"><span class="sentence-number">26</span> <a name="P5S26"></a>In the absence of an <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span>, the attribute applies to the method.</span> <span class="sentence"><span class="sentence-number">27</span> <a name="P5S27"></a>The presence of the method <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the method; the presence of the param <span class="non-terminal"><a href="24.2.htm#attribute-target-specifier">attribute-target-specifier</a></span> indicates that the attribute applies to the parameter.</span> </li></ul></span><span class="locator">
Paragraph 6</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P6S1"></a>An implementation may accept other attribute target specifiers, the purpose of which is implementation-defined.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P6S2"></a>However, an implementation that does not recognize such a target, shall issue a warning.</span> </span><span class="locator">
Paragraph 7</span><span class="paragraph"><span class="sentence"><span class="sentence-number">1</span> <a name="P7S1"></a>By convention, attribute classes are named with a suffix of Attribute.</span> <span class="sentence"><span class="sentence-number">2</span> <a name="P7S2"></a>An <span class="non-terminal"><a href="24.2.htm#attribute-name">attribute-name</a></span> of the form <span class="non-terminal"><a href="10.8.htm#type-name">type-name</a></span> may either include or omit this suffix.</span> <span class="sentence"><span class="sentence-number">3</span> <a name="P7S3"></a>If an attribute class is found both with and without this suffix, an ambiguity is present, and a compile-time error shall be issued.</span> <span class="sentence"><span class="sentence-number">4</span> <a name="P7S4"></a>If the <span class="non-terminal"><a href="24.2.htm#attribute-name">attribute-name</a></span> is spelled using a verbatim identifier (<a href="9.4.2.htm">§9.4.2</a>), then only an attribute without a suffix is matched, thus enabling such an ambiguity to be resolved.</span> <span class="example">[Example: The example <pre class="code-example">
using System;
[AttributeUsage(AttributeTargets.All)]
public class X: Attribute
{}
[AttributeUsage(AttributeTargets.All)]
public class XAttribute: Attribute
{}
[X] // error: ambiguity
class Class1 {}
[XAttribute] // refers to XAttribute
class Class2 {}
[@X] // refers to X
class Class3 {}
[@XAttribute] // refers to XAttribute
class Class4 {}
</pre>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: <pre class="code-example">
using System;
[AttributeUsage(AttributeTargets.All)]
public class XAttribute: Attribute
{}
[X] // refers to XAttribute
class Class1 {}
[XAttribute] // refers to XAttribute
class Class2 {}
[@X] // error: no attribute named "X"
class Class3 {}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -