IL, Metadata tables and Generics.

.Net assemblies contain IL code that your compiler generate from your favorite language and metadata, in form of related tables, that contain all needed data about assembly types and references assemblies. Whidbey introduce generics that enable us to create parametric types (refer to Juval Lowy article). Those types that declare generic parameters enable the class creator to declare constraints on generic parameters. As written in every article on that subject generics embedded deep into CLR thus there must be any modifications to IL and Metadata to reflect generics.

 

There are two new metadata tables that describe generics GenericParam (or 2a and GenericParamConstraint or 2c).  :

 

// =================================================
// 42(0x2a): GenericParam         cRecs:    2(0x2), cbRec: 12(0xc), cbTable:    24(0x18)
//   col  0:  Number       oCol: 0, cbCol:2, USHORT 
//   col  1:  Flags        oCol: 2, cbCol:2, USHORT 
//   col  2:* Owner        oCol: 4, cbCol:2, TypeOrMethodDef
//   col  3:  Name         oCol: 6, cbCol:2, string 
//   col  4:  Kind         oCol: 8, cbCol:2, TypeDefOrRef
//   col  5:  DeprecatedConstraint oCol: a, cbCol:2, TypeDefOrRef
// -------------------------------------------------
//    1 == 0:0000, 1:0000, 2:TypeOrMethodDef[02000002], 3:string#47, 4:TypeDefOrRef[02000000], 5:TypeDefOrRef[02000000]
//    2 == 0:0000, 1:0000, 2:TypeOrMethodDef[06000003], 3:string#89, 4:TypeDefOrRef[02000000], 5:TypeDefOrRef[02000000]
// =================================================
// 44(0x2c): GenericParamConstraint cRecs:    2(0x2), cbRec:  4(0x4), cbTable:     8(0x8)
//   col  0:* Owner        oCol: 0, cbCol:2, GenericParam
//   col  1:  Constraint   oCol: 2, cbCol:2, TypeDefOrRef
// -------------------------------------------------
//    1 == 0:GenericParam[2a000001], 1:TypeDefOrRef[01000001]
//    2 == 0:GenericParam[2a000002], 1:TypeDefOrRef[01000003]
// 

 

GenericParam    Holds all the generics parameters, both of class and method. Column 2 set the owner of the parameter. In our example the first parameter second column point to table 0x2 that holds type definitions thus it’s a Type parameter. The second parameter points to table 0x6 which holds methods definitions thus its Method generic parameter. Column 3 holds generic parameter Name that you set (T). Column 4 set the parameter type which always point to the first entry of type definition table that preserve for Object. DeprecatedConstraint, I can’t figure it up but this column also always points to Objects. (By the way I saw that Mono consider to remove the fourth and fifth parameters)

 

GenericParamConstraint holds constraints, if exist, for Generic parameters. It’s petty simple and small table that contains two columns. The fist column points to entry in GenericParam table containing the constraint parameter. The second column point to entry in TypeRef table that hold definitions of the constraint type.

 

IL uses !x to refer Generic Type parameters where x stand for the position of generic parameter when declared. !!x represent methods generic parameter where x stand for the position of generic parameter when declared. Angle brackets (<>) represent the type chosen for generic parameter when using generic class / method.

 

When you declare generic type and set it generic parameter to certain type the metadata contains the type signature and uses it to display Intellisense and impose type safe. You can see signatures by dumping unresolved externals.

 

// -------------------------------------------------------
//             CallCnvntn: [LOCALSIG]
//             5 Arguments
//                     Argument #1:  GenericInst Class ClassLibrary1.MyCol< I4>
//                     Argument #2:  GenericInst Class ClassLibrary1.MyCol< String>
// 
// Signature #2 (0x11000002)
// -------------------------------------------------------
//             CallCnvntn: [FIELD]
//             Field type:  GenericInst Class ClassLibrary1.MyCol< I4>
// 
// Signature #3 (0x11000003)
// -------------------------------------------------------
//             CallCnvntn: [FIELD]
//             Field type:  GenericInst Class ClassLibrary1.MyCol< String>
// 

 

1 Comment

Comments have been disabled for this content.