13.07.2015 Views

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

C# Language Specification - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>C#</strong> LANGUAGE SPECIFICATIONusing 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]AttributeUsage has another named parameter (§24.1.2), called Inherited, which indicates whether theattribute, when specified on a base class, is also inherited by classes that derive from that base class. IfInherited for an attribute class is true, then that attribute is inherited. If Inherited for an attribute classis false then that attribute is not inherited. If it is unspecified, its default value is true.An attribute class X not having an AttributeUsage attribute attached to it, as inusing System;class X: Attribute { … }is equivalent to the following:using System;[AttributeUsage(AttributeTargets.All, AllowMultiple = false, Inherited =true)]class X: Attribute { … }24.1.2 Positional and named parametersAttribute classes can have positional parameters and named parameters. Each public instance constructorfor an attribute class defines a valid sequence of positional parameters for that attribute class. Each nonstaticpublic read-write field and property for an attribute class defines a named parameter for the attributeclass.[Example: The exampleusing System;[AttributeUsage(AttributeTargets.Class)]public class HelpAttribute: Attribute{public HelpAttribute(string url) { // url is a positional parameter…}public string Topic { // Topic is a named parameterget {…}set {…}}public string Url { get {…} }}defines an attribute class named HelpAttribute that has one positional parameter (string url) and onenamed parameter (string Topic). Although it is non-static and public, the property Url does not define anamed parameter, since it is not read-write.This attribute class might be used as follows:[Help("http://www.mycompany.com/…/Class1.htm")]class Class1{}306

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!