Skip to content

Latest commit

 

History

History
63 lines (45 loc) · 3.6 KB

ii.17-defining-properties.md

File metadata and controls

63 lines (45 loc) · 3.6 KB

II.17 Defining properties

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

ClassMember ::=
.property PropHeader '{' PropMember* '}'

See §II.22.34 and §II.22.35 for how property information is stored in metadata.

PropHeader ::=
[ specialname ] [ rtspecialname ] CallConv Type Id '(' Parameters ')'

The .property directive specifies a calling convention (§II.15.3), type, name, and parameters 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 PropHeader 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). end rationale]

While the CLI places no constraints on the methods that make up a property, the CLS (see Partition I) specifies a set of consistency constraints. A property can contain any number of methods in its body. The following table shows how these methods are identified, and provides short descriptions of each kind of item:

PropMember ::= Description Clause
| .custom CustomDecl Custom attribute. §II.21
| .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 §II.5.7

.get specifies the getter for this property. The TypeSpec defaults to the current type. Only one getter can 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 can 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 (§II.21) or source line declarations can be specified.

[Example: This shows the declaration of the 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 MyCount::get_Count()
    .set instance void MyCount::set_Count(int32)
    .other instance void MyCount::reset_Count()
  }
}

end example]