16 Defining Properties


A property is declared by the using the .property directive. Properties may only be declared inside of types (i.e., global properties are not supported).

<classMember> ::=

 

.property <propHead> { <propMember>* }

See Partition II, sections 21.31 and 21.32 for how Property information is stored in metadata.

<propHead> ::=

 

[specialname][rtspecialname] <callConv> <type> <id> ( <parameters> )

The property directive specifies a calling convention (see Partition II, section 14.3), type, name, and parameter in parentheses. specialname marks the property as special to other tools, while rtspecialname marks the property as special to the CLI. The signature for the property (i.e., the <propHead> production) shall match the signature of the property's .get method (see below).

RATIONALE

There are currently no property names that are required to be marked with rtspecialname. It is provided for extensions, future standardization, and to increase consistency between the declaration of properties and methods (instance and type initializer methods shall be marked with this attribute).


While the CLI places no constraints on the methods that make up a property, the CLS (see Partition I, section 8.11.3) specifies a set of consistency constraints..

A property may contain any number of methods in its body. The following table shows these and provides short descriptions of each item:

<propMember> ::=

Description

Section in Partition II

| .custom <customDecl>

Custom attribute.

20

| .get <callConv> <type> [<typeSpec> ::] <methodName> ( <parameters> )

Specifies the getter for the property.

 

| .other <callConv> <type> [<typeSpec> ::] <methodName> ( <parameters> )

Specifies a method for the property other than the getter or setter.

 

| .set <callConv> <type> [<typeSpec> ::] <methodName> ( <parameters> )

Specifies the setter for the property.

 

| <externSourceDecl>

.line or #line

5.7

.get specifies the getter for this property. The <typeSpec> defaults to the current type. Only one getter may be specified for a property. To be CLS-compliant, the definition of getter shall be marked specialname.

.set specifies the setter for this property. The <typeSpec> defaults to the current type. Only one setter may be specified for a property. To be CLS-compliant, the definition of setter shall be marked specialname.

.other is used to specify any other methods that this property comprises.

In addition, custom attributes (see Partition II, section 20) or source line declarations may be specified.

 
 Example (informative): This example shows the declaration of a property called Count. .class public auto autochar MyCount extends [mscorlib]System.Object {      .method virtual hidebysig public specialname instance int32 get_Count() {             // body of getter      }      .method virtual hidebysig public specialname instance void set_Count(int32 newCount) {            // body of setter      }      .method virtual hidebysig public instance void reset_Count() {            // body of refresh method      }      // the declaration of the property     .property int32 Count() {             .get instance int32 get_Count()             .set instance void set_Count(int32)             .other instance void reset_Count()      } } 

ANNOTATION

Properties do not have any special existence in the VES. The methods associated with properties (getter, setter, and any other) carry the information that allow compilers to understand that a property is a property, and to deal with it appropriately. That is why the getter and setter methods must be marked with specialname to be CLS-compliant. That designation indicates that they are special to the compiler.




The Common Language Infrastructure Annotated Standard (Microsoft. NET Development Series)
The Common Language Infrastructure Annotated Standard (Microsoft. NET Development Series)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 121

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net