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.

Chapter 24 Attributesusing System;[AttributeUsage(AttributeTargets.Class)]public class MyAttribute: Attribute{public int P1 {}}end example]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 {}24.3 Attribute instancesAn attribute instance is an instance that represents an attribute at run-time. An attribute is defined with anattribute class, positional arguments, and named arguments. An attribute instance is an instance of theattribute 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 thefollowing sections.24.3.1 Compilation of an attributeThe 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 formnew T(P). These steps either result in a compile-time error, or determine an instance constructor on Tthat can be invoked at run-time. Call this instance constructor C.• If C 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: the attribute class T, theinstance constructor C on T, the positional-argument-list P and the named-argument-list N.24.3.2 Run-time retrieval of an attribute instanceCompilation of an attribute yields an attribute class T, an instance constructor C on T, a positional-argumentlistP, and a named-argument-list N. Given this information, an attribute instance can be retrieved at run-timeusing the following steps:• Follow the run-time processing steps for executing an object-creation-expression of the formnew T(P), using the instance constructor C as determined at compile-time. These steps either result inan exception, or produce an instance of T. Call this instance O.• For each named-argument Arg in N, in order:311

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

Saved successfully!

Ooh no, something went wrong!