Property Metadata

Property Metadata

Properties are considerably less fascinating than events. Typically, a property is some characteristic of the class that declares it—for example, the value of a private field—accessible only through the so-called accessor methods. Because of this, the only aspects of a property the common language runtime is concerned with are the property’s accessors.

Let’s suppose that a property is based on a private field. Let’s also suppose that both read and write accessors are defined. What is the sense in declaring such a property, when we could simply make the field public and be done with it? At least two reasons argue for declaring it: the accessors can run additional checks to ensure that the field has valid values at all times, and the accessors can fire events signaling that the property has been changed or accessed. I’m sure you can think of other reasons for implementing properties, even leaving aside cases in which the property is not field-based or has only a read accessor or only a write accessor.

A property’s read and write accessors are referred to as getters and setters, respectively. The Visual C# .NET and Visual Basic .NET compilers follow these naming conventions for the property accessors: setters are named set_<property_name>, and getters are named get_<property_name>. Both methods are marked with the specialname flag.

The property metadata group includes the following tables: Property, PropertyMap, TypeDef, Method, MethodSemantics, and Constant. The structure of the property metadata group is shown in Figure 12-3. The following sections describe the Property and PropertyMap tables. The MethodSemantics table was discussed in the preceding section of this chapter, and Chapter 8, “Fields and Data Constants,” contains information about the Constant table.

Figure 12-3 The property metadata group.

The Property Table

The Property table has the associated token type mdtProperty (0x17000000), and its records have three entries:

  • PropFlags (2-byte unsigned integer)  Binary flags of the property characteristics.

  • Name (offset in the #Strings stream)  The name of the property, which must be a simple name no longer than 1023 bytes in UTF-8 encoding.

  • Type (offset in the #Blob stream)  The property signature.

The Type entry holds an offset to the property signature residing in the #Blob metadata stream. The structure of the property signature is similar to that of the method signature, except that the calling convention is IMAGE_CEE_CS_ CALLCONV_PROPERTY (0x08). The return type of the property should correspond to those of the getter. The runtime, of course, pays no attention to what the property signature looks like, but the compilers do care.

Three flag values are defined for properties, and, as in the case of events, only one of them can be set explicitly:

  • specialname (0x0200)  The property is special in some way, as specified by the name.

  • rtspecialname (0x0400)  The event has a special name reserved for the internal use of the common language runtime. This flag can’t be set explicitly.

  • [no ILAsm keyword] (0x1000)  The property has a default value, which resides in the Constant table.

Like the event flags, the specialname and rtspecialname flags are used by the runtime for marking deleted properties in edit-and-continue scenarios. The deleted property name is changed to _Deleted. The flag 0x1000 is set by the metadata emission API when a Constant record is emitted for this property, signifying the property’s default value.

The PropertyMap Table

The PropertyMap table serves the same purpose for properties as the EventMap table does for events: it provides mapping between the TypeDef table and the Property table. A PropertyMap record has two entries:

  • Parent (RID to the TypeDef table)  The type declaring the properties.

  • PropertyList (RID to the Property table)  The beginning of the properties declared by the type referenced by the Parent entry.

In the unoptimized model (the #- stream), an intermediate lookup metadata table, PropertyPtr, is used to remap the properties so that they are ordered by parent.



Inside Microsoft. NET IL Assembler
Inside Microsoft .NET IL Assembler
ISBN: 0735615470
EAN: 2147483647
Year: 2005
Pages: 147
Authors: SERGE LIDIN

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