Jon Jagger
jon@jaggersoft.com
Table of Contents 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Notes DownloadECMA-334 C# Language Specificationpreviousnextprevious at this levelnext at this level 24 Attributesprevious at this levelnext at this level 24.1 Attribute classesprevious at this levelnext at this level 24.1.1 Attribute usage Paragraph 11 The attribute AttributeUsage (§24.4.1) is used to describe how an attribute class can be used. Paragraph 21 AttributeUsage has a positional parameter (§24.1.2) that enables an attribute class to specify the kinds of declarations on which it can be used. [Example: The example
using System;  
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]  
public class SimpleAttribute: Attribute  
{}  
defines an attribute class named SimpleAttribute that can be placed on class-declarations and interface-declarations only. The example
[Simple] class Class1 {...}  
[Simple] interface Interface1 {...}  
shows several uses of the Simple attribute. Although this attribute is defined with the name SimpleAttribute, when this attribute is used, the Attribute suffix may be omitted, resulting in the short name Simple. Thus, the example above is semantically equivalent to the following
[SimpleAttribute] class Class1 {...}  
[SimpleAttribute] interface Interface1 {...}  
end example]
Paragraph 31 AttributeUsage has a named parameter (§24.1.2), called AllowMultiple, which indicates whether the attribute can be specified more than once for a given entity. 2 If AllowMultiple for an attribute class is true, then that class is a multi-use attribute class, and can be specified more than once on an entity. 3 If AllowMultiple for an attribute class is false or it is unspecified, then that class is a single-use attribute class, and can be specified at most once on an entity. [Example: The example
using System;  
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]  
public class AuthorAttribute: Attribute {  
   public AuthorAttribute(string name) {  
      this.name = name;  
   }  
   public string Name { get { return name;} }  
   private string name;  
}  
defines a multi-use attribute class named AuthorAttribute. The example
[Author("Brian Kernighan"), Author("Dennis Ritchie")]   
class Class1 {...}  
shows a class declaration with two uses of the Author attribute. end example]
Paragraph 41 AttributeUsage has another named parameter (§24.1.2), called Inherited, which indicates whether the attribute, when specified on a base class, is also inherited by classes that derive from that base class. 2 If Inherited for an attribute class is true, then that attribute is inherited. 3 If Inherited for an attribute class is false or it is unspecified, then that attribute is not inherited. Paragraph 51 An attribute class X not having an AttributeUsage attribute attached to it, as in
using System;  
class X: Attribute { ... }  
is equivalent to the following:
using System;  
[AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited =  
true)]  
class X: Attribute { ... }  
{ JSL }
Jagger Software Ltd
Company # 4070126
VAT # 762 5213 42
Valid HTML 4.01Valid CSS