attributes attribute specification.md - brainchildservices/curriculum GitHub Wiki

Slide 1

Attribute specification

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 or module) and for type_declarations (Type declarations), class_member_declarations (Type parameter constraints), interface_member_declarations (Interface members), struct_member_declarations (Struct members), enum_member_declarations (Enum members), accessor_declarations (Accessors), event_accessor_declarations (Field-like events), and formal_parameter_lists (Method parameters).

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.

  global_attributes
      : global_attribute_section+
      ;

  global_attribute_section
      : '[' global_attribute_target_specifier attribute_list ']'
      | '[' global_attribute_target_specifier attribute_list ',' ']'
      ;

  global_attribute_target_specifier
      : global_attribute_target ':'
      ;

  global_attribute_target
      : 'assembly'
      | 'module'
      ;

  attributes
      : attribute_section+
      ;

  attribute_section
      : '[' attribute_target_specifier? attribute_list ']'
      | '[' attribute_target_specifier? attribute_list ',' ']'
      ;

  attribute_target_specifier
      : attribute_target ':'
      ;

  attribute_target
      : 'field'
      | 'event'
      | 'method'
      | 'param'
      | 'property'
      | 'return'
      | 'type'
      ;

  attribute_list
      : attribute (',' attribute)*
      ;

  attribute
      : attribute_name attribute_arguments?
      ;

  attribute_name
      : type_name
      ;

  attribute_arguments
      : '(' positional_argument_list? ')'
      | '(' positional_argument_list ',' named_argument_list ')'
      | '(' named_argument_list ')'
      ;

  positional_argument_list
      : positional_argument (',' positional_argument)*
      ;

  positional_argument
      : attribute_argument_expression
      ;

  named_argument_list
      : named_argument (','  named_argument)*
      ;

  named_argument
      : identifier '=' attribute_argument_expression
      ;

  attribute_argument_expression
      : expression
      ;

Slide 2

An attribute consists of an attribute_name 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 attribute_argument_expression, which, together, are constrained by the same rules as simple assignment. The order of named arguments is not significant.

The attribute_name identifies an attribute class. If the form of attribute_name is type_name then this name must refer to an attribute class. Otherwise, a compile-time error occurs. The example

   class Class1 {}

   [Class1] class Class2 {}    // Error

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

Slide 3

Certain contexts permit the specification of an attribute on more than one target. A program can explicitly specify the target by including an attribute_target_specifier. When an attribute is placed at the global level, a global_attribute_target_specifier is required. In all other locations, a reasonable default is applied, but an attribute_target_specifier 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, attribute_target_specifiers can be omitted except at the global level. The potentially ambiguous contexts are resolved as follows:

  • An attribute specified at global scope can apply either to the target assembly or the target module. No default exists for this context, so an attribute_target_specifier is always required in this context. The presence of the assembly attribute_target_specifier indicates that the attribute applies to the target assembly; the presence of the module attribute_target_specifier indicates that the attribute applies to the target module.
  • 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 attribute_target_specifier, the attribute applies to the delegate. The presence of the type attribute_target_specifier indicates that the attribute applies to the delegate; the presence of the return attribute_target_specifier indicates that the attribute applies to the return value.
  • 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 attribute_target_specifier, the attribute applies to the method. The presence of the method attribute_target_specifier indicates that the attribute applies to the method; the presence of the return attribute_target_specifier indicates that the attribute applies to the return value.
  • An attribute specified on an operator declaration can apply either to the operator being declared or to its return value. In the absence of an attribute_target_specifier, the attribute applies to the operator. The presence of the method attribute_target_specifier indicates that the attribute applies to the operator; the presence of the return attribute_target_specifier indicates that the attribute applies to the return value.

Slide 3 Downwards

  • 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 attribute_target_specifier, the attribute applies to the event. The presence of the event attribute_target_specifier indicates that the attribute applies to the event; the presence of the field attribute_target_specifier indicates that the attribute applies to the field; and the presence of the method attribute_target_specifier indicates that the attribute applies to the methods.
  • 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 attribute_target_specifier, the attribute applies to the method. The presence of the method attribute_target_specifier indicates that the attribute applies to the method; the presence of the return attribute_target_specifier indicates that the attribute applies to the return value.
  • 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 attribute_target_specifier, the attribute applies to the method. The presence of the method attribute_target_specifier indicates that the attribute applies to the method; the presence of the param attribute_target_specifier indicates that the attribute applies to the parameter; the presence of the return attribute_target_specifier indicates that the attribute applies to the return value.
  • 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 attribute_target_specifier, the attribute applies to the method. The presence of the method attribute_target_specifier indicates that the attribute applies to the method; the presence of the param attribute_target_specifier indicates that the attribute applies to the parameter; the presence of the return attribute_target_specifier indicates that the attribute applies to the return value.

For more Visit:- https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes#attribute-specification