[ Pobierz całość w formacie PDF ]
( positional-argument-list , named-argument-list )
( named-argument-list )
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 243
C# LANGUAGE REFERENCE
positional-argument-list:
positional-argument
positional-argument-list , positional-argument
positional-argument:
attribute-argument-expression
named-argument-list:
named-argument
named-argument-list , named-argument
named-argument:
identifier = attribute-argument-expression
attribute-argument-expression:
expression
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.
The attribute-name identifies either a reserved attribute or 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
is in error because it attempts to use Class1, which is not an attribute class, as an attribute class.
It is an error to use a single-use attribute class more than once on the same entity. The example
[AttributeUsage(AttributeTargets.Class)]
public class HelpStringAttribute: System.Attribute
{
string value;
public HelpStringAttribute(string value) {
this.value = value;
}
public string Value { get {& } }
}
[HelpString("Description of Class1")]
[HelpString("Another description of Class1")]
public class Class1 {}
is in error because it attempts to use HelpString, which is a single-use attribute class, more than once on the
declaration of Class1.
An expression E is an attribute-argument-expression if all of the following statements are true:
" The type of E is an attribute parameter type (§17.1.3).
" At compile-time, the value of E can be resolved to one of the following:
" A constant value.
" A System.Type object.
" A one-dimensional array of attribute-argument-expressions.
244 Copyright Microsoft Corporation 1999-2000. All Rights Reserved.
Chapter 17 Attributes
17.3 Attribute instances
An attribute instance is an instance that represents an attribute at run-time. An attribute is defined with an
attribute, positional arguments, and named arguments. An attribute instance is an instance of the attribute class
that is initialized with the positional and named arguments.
Retrieval of an attribute instance involves both compile-time and run-time processing, as described in the
following sections.
17.3.1 Compilation of an attribute
The compilation of an attribute with attribute class T, positional-argument-list P and named-argument-list N,
consists of the following steps:
" Follow the compile-time processing steps for compiling an object-creation-expression of the form new
T(P). These steps either result in a compile-time error, or determine a constructor on T that can be invoked
at run-time. Call this constructor C.
" If the constructor determined in the step above does not have public accessibility, then a compile-time error
occurs.
" For each named-argument Arg in N:
" Let Name be the identifier of the named-argument Arg.
" Name must identify a non-static read-write public field or property on T. If T has no such field or
property, then a compile-time error occurs.
" Keep the following information for run-time instantiation of the attribute instance: the attribute class T, the
constructor C on T, the positional-argument-list P and the named-argument-list N.
17.3.2 Run-time retrieval of an attribute instance
Compilation of an attribute yields an attribute class T, constructor C on T, positional-argument-list P and
named-argument-list N. Given this information, an attribute instance can be retrieved at run-time using the
following steps:
" Follow the run-time processing steps for executing an object-creation-expression of the form T(P), using
the constructor C as determined at compile-time. These steps either result in an exception, or produce an
instance of T. Call this instance O.
" For each named-argument Arg in N, in order:
" Let Name be the identifier of the named-argument Arg. If Name does not identify a non-static public
read-write field or property on O, then an exception (TODO: which exception?) is thrown.
" Let Value be the result of evaluating the attribute-argument-expression of Arg.
" If Name identifies a field on O, then set this field to the value Value.
" Otherwise, Name identifies a property on O. Set this property to the value Value.
" The result is O, an instance of the attribute class T that has been initialized with the positional-argument-
list P and the named-argument-list N.
17.4 Reserved attributes
A small number of attributes affect the language in some way. These attributes include:
Copyright Microsoft Corporation 1999-2000. All Rights Reserved. 245
C# LANGUAGE REFERENCE
" System.AttributeUsageAttribute, which is used to describe the ways in which an attribute class can
be used.
" System.ConditionalAttribute, which is used to define conditional methods.
" System.ObsoleteAttribute, which is used to mark a member as obsolete.
17.4.1 The AttributeUsage attribute
The AttributeUsage attribute is used to describe the manner in which the attribute class can be used.
A class that is decorated with the AttributeUsage attribute must derive from System.Attribute, either
directly or indirectly. Otherwise, a compile-time error occurs.
[AttributeUsage(AttributeTargets.Class)]
public class AttributeUsageAttribute: System.Attribute
{
public AttributeUsageAttribute(AttributeTargets validOn) {& }
public AttributeUsageAttribute(AttributeTargets validOn,
bool allowMultiple,
bool inherited) {& }
public bool AllowMultiple { virtual get {& } virtual set {& } }
public bool Inherited { virtual get {& } virtual set {& } }
public AttributeTargets ValidOn { virtual get {& } }
}
public enum AttributeTargets
{
Assembly = 0x0001,
Module = 0x0002,
Class = 0x0004,
Struct = 0x0008,
Enum = 0x0010,
Constructor = 0x0020,
Method = 0x0040,
Property = 0x0080,
Field = 0x0100,
Event = 0x0200,
Interface = 0x0400,
Parameter = 0x0800,
Delegate = 0x1000,
All = Assembly | Module | Class | Struct | Enum | Constructor |
Method | Property | Field | Event | Interface | Parameter |
Delegate,
ClassMembers = Class | Struct | Enum | Constructor | Method |
Property | Field | Event | Delegate | Interface,
}
[ Pobierz całość w formacie PDF ]