Inside Microsoft .NET IL Assembler

Class Metadata

From a structural point of view, all five categories of types have identical metadata representations. Thus we can talk about class metadata, or type metadata, in a general sense.

Class metadata is grouped around two distinct concepts: type definition (TypeDef) and type reference (TypeRef). TypeDefs and related metadata describe the types declared in the current module, whereas TypeRefs describe references to types that are declared somewhere else. Because it obviously takes more information to adequately define a type than to refer to one already defined, TypeDefs and related metadata are far more complex than TypeRefs.

When defining a type, you should supply the following information:

When referencing a type, only its name and resolution scope need be specified. The resolution scope indicates where the definition of the referenced type can be found: in this module, in another module of this assembly, or in another assembly. In the case of referencing the nested types, the resolution scope is another TypeRef.

Figure 6-2 shows the metadata tables that engage in type definition and referencing but not the tables related to identification of type members—fields and methods, for example, and their attributes. The arrows denote cross-table referencing by means of metadata tokens. In the following sections, we’ll have a look at all the metadata tables involved.

Figure 6-2 Metadata tables that engage in type definition and referencing.

TypeDef Metadata Table

The TypeDef table is the main table containing type definition information. Each record in this table has six entries:

TypeRef Metadata Table

The TypeRef metadata table has a much simpler structure than the TypeDef table. Each record in this table has three entries:

InterfaceImpl Metadata Table

If the defined type implements one or several interfaces, the corresponding TypeDef record is referenced in one or several records of the InterfaceImpl metadata table. This table serves as a lookup table, providing information about “what is implementing what,” and it is ordered by implementing type. The InterfaceImpl table has only two entries in each record:

NestedClass Metadata Table

If the defined type is nested in another type, its TypeDef record is referenced in another lookup table: the NestedClass metadata table. (For more information about nesting, see “Nested Types” later in this chapter.) Like the InterfaceImpl table, the NestedClass table has only two entries per record:

Because types of both entries are RIDs in the TypeDef table, the nestee and the encloser cannot be defined in different modules or assemblies.

ClassLayout Metadata Table

Usually, the loader has its own ideas about how to lay out the type being loaded. Certain types, however, must be laid out in a specific manner, and they carry metadata information regarding these specifics.

The ClassLayout metadata table provides additional information about the packing order and total size of the type. In the section “Value Type as Placeholder” in Chapter 1, for example, when we declared a “placeholder” type without any internal structure, we used such additional information—the total size of the type.

A record in the ClassLayout metadata table has three entries:

Категории