class Class1 {} [Class1] class Class2 {} // Errorresults in a compile-time error because it attempts to use Class1 as an attribute class when Class1 is not an attribute class. end example] Paragraph 51 Certain contexts permit the specification of an attribute on more than one target. 2 A program can explicitly specify the target by including an attribute-target-specifier. 3 When an attribute is placed at the global level, a global-attribute-target-specifier is required. 4 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). 5 Thus, typically, attribute-target-specifiers can be omitted except at the global level. 6 The potentially ambiguous contexts are resolved as follows:
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 {}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:
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 {}end example] Paragraph 81 It is a compile-time error to use a single-use attribute class more than once on the same entity. [Example: The example
using System; [AttributeUsage(AttributeTargets.Class)] public class HelpStringAttribute: 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 {}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] Paragraph 91 An expression E is an attribute-argument-expression if all of the following statements are true:
using System; [AttributeUsage(AttributeTargets.Class)] public class MyAttribute: Attribute { public int P1 { get {...} set {...} } public Type P2 { get {...} set {...} } public object P3 { get {...} set {...} } } [My(P1 = 1234, P3 = new int[]{1, 3, 5}, P2 = typeof(float))] class MyClass {}end example]
| |
Jagger Software Ltd | |
Company # 4070126 | |
VAT # 762 5213 42 |