Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

Chapter 24 Attributes

1defines an attribute class named HelpAttribute that has one positional parameter (string url) and one

2named parameter (string Topic). Although it is non-static and public, the property Url does not define a

3named parameter, since it is not read-write.

4This attribute class might be used as follows:

5[Help("http://www.mycompany.com/…/Class1.htm")]

6class Class1

7{

8}

9[Help("http://www.mycompany.com/…/Misc.htm", Topic ="Class2")]

10class Class2

11{

12}

13end example]

1424.1.3 Attribute parameter types

15The types of positional and named parameters for an attribute class are limited to the attribute parameter

16types, which are:

17One of the following types: bool, byte, char, double, float, int, long, short, string.

18The type object.

19The type System.Type.

20An enum type, provided it has public accessibility and the types in which it is nested (if any) also have

21public accessibility.

22Single-dimensional arrays of the above types.

2324.2 Attribute specification

24Attribute specification is the application of a previously defined attribute to a declaration. An attribute is a

25piece of additional declarative information that is specified for a declaration. Attributes can be specified at

26global scope (to specify attributes on the containing assembly) and for type-declarations (§16.6), class-

27member-declarations (§17.1.4), struct-member-declarations (§18.2), interface-member-declarations

28(§20.2), enum-member-declarations (§21.1), accessor-declarations (§17.6.2), event-accessor-declarations

29(§17.7), elements of formal-parameter-lists (§17.5.1), and elements of type-parameter-lists (§26.1.1).

30Attributes are specified in attribute sections. An attribute section consists of a pair of square brackets, which

31surround a comma-separated list of one or more attributes. The order in which attributes are specified in

32such a list, and the order in which sections attached to the same program entity are arranged, is not

33significant. For instance, the attribute specifications [A][B], [B][A], [A, B], and [B, A] are equivalent.

34global-attributes:

35

global-attribute-sections

36

global-attribute-sections:

37

global-attribute-section

38

global-attribute-sections global-attribute-section

39

global-attribute-section:

40

[ global-attribute-target-specifier attribute-list ]

41

[ global-attribute-target-specifier attribute-list , ]

42

global-attribute-target-specifier:

43

global-attribute-target :

44

global-attribute-target:

45

identifier-or-keyword

367

 

C# LANGUAGE SPECIFICATION

 

1

attributes:

 

2

attribute-sections

 

3

attribute-sections:

 

4

attribute-section

 

5

attribute-sections attribute-section

6

attribute-section:

 

7

[ attribute-target-specifieropt

attribute-list ]

8

[ attribute-target-specifieropt attribute-list , ]

9

attribute-target-specifier:

 

10

attribute-target :

 

11

attribute-target:

 

12

identifier-or-keyword

 

13

attribute-list:

 

14

attribute

 

15

attribute-list , attribute

 

16

attribute:

 

17

attribute-name attribute-argumentsopt

18

attribute-name:

 

19

type-name

 

20

attribute-arguments:

 

21

( positional-argument-listopt

)

22

( positional-argument-list ,

named-argument-list )

23( named-argument-list )

24positional-argument-list:

25

positional-argument

26

positional-argument-list , positional-argument

27

positional-argument:

28

attribute-argument-expression

29

named-argument-list:

30

named-argument

31

named-argument-list , named-argument

32

named-argument:

33

identifier = attribute-argument-expression

34

attribute-argument-expression:

35

expression

36An attribute consists of an attribute-name and an optional list of positional and named arguments. The

37positional arguments (if any) precede the named arguments. A positional argument consists of an attribute-

38argument-expression; a named argument consists of a name, followed by an equal sign, followed by an

39attribute-argument-expression, which, together, are constrained by the same rules as simple assignment. The

40order of named arguments is not significant.

41[Note: For convenience, a trailing comma is allowed in a global-attribute-section and an attribute-section,

42just as one is allowed in an array-initializer (§19.6). end note]

43The attribute-name identifies an attribute class. type-name shall refer to an attribute class. Otherwise, a

44compile-time error occurs. [Example: The example

45class Class1 {}

46

[Class1] class Class2 {} // Error

368

Chapter 24 Attributes

1results in a compile-time error because it attempts to use Class1 as an attribute class when Class1 is not

2an attribute class. end example]

3When an attribute is placed at the global level, a global-attribute-target-specifier is required. The only

4standardized global-attribute-target name is assembly. This target name shall only be used in the context

5of an assembly.

6The only standardized attribute-target names are event, field, method, param, property, return, and

7type. These target names shall only be used in the following contexts:

8• event — an event.

9• field — a field. A field-like event (i.e., one without accessors) can also have an attribute with this

10target.

11• method — a constructor, destructor, method, operator, property get and set accessors, and event add and

12remove accessors. A field-like event (i.e., one without accessors) can also have an attribute with this

13target.

14• param — a property set accessor, event add and remove accessors, and a parameter in a constructor,

15method, and operator.

16• property — a property.

17• return — a delegate, method, operator, and property get accessor.

18type — a delegate, class, struct, and interface.

19typevar — a type parameter.

20Certain contexts permit the specification of an attribute on more than one target. A program can explicitly

21specify the target by including an attribute-target-specifier. In the absence of an attribute-target-specifier, a

22reasonable default is applied, but an attribute-target-specifier can be used to affirm or override the default in

23certain ambiguous cases (or to just affirm the default in non-ambiguous cases). Thus, typically, attribute-

24target-specifiers can be omitted. The potentially ambiguous contexts are resolved as follows (using equality

25as defined in §9.4.2):

26An attribute specified on a delegate declaration can apply either to the delegate being declared or to its

27return value. In the absence of an attribute-target-specifier, the attribute applies to the delegate. An

28attribute-target equal to type indicates that the attribute applies to the delegate; an attribute-target

29equal to return indicates that the attribute applies to the return value.

30An attribute specified on a method declaration can apply either to the method being declared or to its

31return value. In the absence of an attribute-target-specifier, the attribute applies to the method. An

32attribute-target equal to method indicates that the attribute applies to the method; an attribute-target

33equal to return indicates that the attribute applies to the return value.

34An attribute specified on an operator declaration can apply either to the operator being declared or to its

35return value. In the absence of an attribute-target-specifier, the attribute applies to the operator. An

36attribute-target equal to method indicates that the attribute applies to the operator; an attribute-target

37equal to return indicates that the attribute applies to the return value.

38An attribute specified on a get accessor declaration for a property or indexer declaration can apply either

39to the associated method or to its return value. In the absence of an attribute-target-specifier, the

40attribute applies to the method. An attribute-target equal to method indicates that the attribute applies to

41the method; an attribute-target equal to return indicates that the attribute applies to the return value.

42An attribute specified on a set accessor for a property or indexer declaration can apply either to the

43associated method or to its lone implicit parameter. In the absence of an attribute-target-specifier, the

44attribute applies to the method. An attribute-target equal to method indicates that the attribute applies to

45the method; the presence of an attribute-target equal to param indicates that the attribute applies to the

46parameter.

369

C# LANGUAGE SPECIFICATION

1An attribute specified on an event declaration that omits event-accessor-declarations can apply to the

2event being declared, to the associated field (if the event is not abstract), or to the associated add and

3remove methods. In the absence of an attribute-target-specifier, the attribute applies to the event

4declaration. An attribute-target equal to event indicates that the attribute applies to the event; an

5attribute-target equal to field indicates that the attribute applies to the field; and an attribute-target

6equal to method indicates that the attribute applies to the methods.

7In the case of an event declaration that does not omit event-accessor-declarations, an attribute specified

8on an add or remove accessor declaration for an event declaration can apply either to the associated

9method or to its lone parameter. In the absence of an attribute-target-specifier, the attribute applies to

10the method. An attribute-target equal to method indicates that the attribute applies to the method; an

11attribute-target equal to param indicates that the attribute applies to the parameter.

12An implementation can accept other attribute target specifiers, the purposes of which are implementation-

13defined. However, an implementation that does not recognize such a target, shall issue a warning.

14By convention, attribute classes are named with a suffix of Attribute. An attribute-name can either

15include or omit this suffix. Specifically, an attribute-name is resolved as follows:

16If the right-most identifier of the attribute-name is a verbatim identifier (§9.4.2), then the attribute-name

17is resolved as a type-name (§10.8). If the result is not a type derived from System.Attribute, a

18compile-time error occurs.

19Otherwise,

20o The attribute-name is resolved as a type-name (§10.8) except any errors are suppressed. If this

21resolution is successful and results in a type derived from System.Attribute then the type is the

22result of this step.

23o The characters Attribute are appended to the right-most identifier in the attribute-name and the

24resulting string of tokens is resolved as a type-name (§10.8) except any errors are suppressed. If this

25resolution is successful and results in a type derived from System.Attribute then the type is the

26result of this step.

27If exactly one of the two steps above results in a type derived from System.Attribute, then that type

28is the result of the attribute-name. Otherwise a compile-time error occurs.

29[Example: Informally, when attempting to resolve an attribute-name, if an attribute class is found both with

30and without the Attribute suffix, an ambiguity is present, and a compile-time error is issued. If the

31attribute-name is spelled such that its right-most identifier is a verbatim identifier (§9.4.2), then only an

32attribute without a suffix is matched, thus enabling such an ambiguity to be resolved. The example

33using System;

34[AttributeUsage(AttributeTargets.All)]

35public class X: Attribute

36{}

37[AttributeUsage(AttributeTargets.All)]

38public class XAttribute: Attribute

39{}

40

[X]

// error: ambiguity

41

class Class1 {}

 

42

[XAttribute]

// refers to XAttribute

43

class Class2 {}

 

44

[@X]

// refers to X

45

class Class3 {}

 

46

[@XAttribute]

// refers to XAttribute

47class Class4 {}

48shows two attribute classes named X and XAttribute. The attribute reference [X] is ambiguous, since it

49could refer to either X or XAttribute. Using a verbatim identifier allows the exact intent to be specified in

50such rare cases. The attribute reference [XAttribute] is not ambiguous (although it would be if there were

370

Chapter 24 Attributes

1an attribute class named XAttributeAttribute!). If the declaration for class X is removed, then both

2attributes refer to the attribute class named XAttribute, as follows:

3using System;

4[AttributeUsage(AttributeTargets.All)]

5public class XAttribute: Attribute

6{}

7

[X]

// refers to XAttribute

8

class Class1 {}

 

9

[XAttribute]

// refers to XAttribute

10

class Class2 {}

 

11

[@X]

// error: no attribute named “X”

12class Class3 {}

13end example]

14It is a compile-time error to use a single-use attribute class more than once on the same entity. [Example:

15The example

16using System;

17[AttributeUsage(AttributeTargets.Class)]

18public class HelpStringAttribute: Attribute

19{

20

string value;

21

public HelpStringAttribute(string value) {

22

this.value = value;

23

}

24public string Value { get {…} }

25}

26[HelpString("Description of Class1")]

27[HelpString("Another description of Class1")]

28public class Class1 {}

29results in a compile-time error because it attempts to use HelpString, which is a single-use attribute class,

30more than once on the declaration of Class1. end example]

31An expression E is an attribute-argument-expression if all of the following statements are true:

32The type of E is an attribute parameter type (§24.1.3).

33At compile-time, the value of E can be resolved to one of the following:

34o A constant value.

35o Atypeof-expression (§14.5.11) specifying a non-generic type, a closed constructed type (§26.5.2), or

36an unbound generic type (§26.5).

37o A one-dimensional array of attribute-argument-expressions.

38[Example:

39using System;

40[AttributeUsage(AttributeTargets.Class)]

41public class MyAttribute: Attribute

42{

43

public

int P1 {

44

get {…}

45

set {…}

46

}

 

47

public Type P2 {

48

get {…}

49

set {…}

50

}

 

371

Соседние файлы в предмете Электротехника