CHAPTER 18 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES
Defining Generics in CIL
Generic types also have a specific representation in the syntax of CIL. Recall from Chapter 9 that a given
generic type or generic member may have one or more type parameters. For example, the List type
has a single type parameter, while Dictionary has two. In terms of CIL, the number of
type parameters is specified using a backward-leaning single tick, `, followed by a numerical value
representing the number of type parameters. Like C#, the actual value of the type parameters is encased
within angled brackets.
Note On most keyboards, the ` character can be found on the key above the Tab key (and to the left of the 1 key).
For example, assume you wish to create a List variable, where T is of type System.Int32. In CIL,
you would author the following (which could appear in any CIL method scope):
// In C#: List myInts = new List();
newobj instance void class [mscorlib]
System.Collections.Generic.List`1::.ctor()
Notice that this generic class is defined as List`1, as List has a single type parameter.
However, if you needed to define a Dictionarytype, you would do so as the following:
// In C#: Dictionary d = new Dictionary();
newobj instance void class [mscorlib]
System.Collections.Generic.Dictionary`2::.ctor()
As another example, if you have a generic type that uses another generic type as a type parameter,
you would author CIL code such as the following:
// In C#: List> myInts = new List>();
newobj instance void class [mscorlib]
System.Collections.Generic.List`1>::.ctor()
Compiling the CILTypes.il file
Even though you have not yet added any members or implementation code to the types you have
defined, you are able to compile this *.il file into a .NET DLL assembly (which you must do, as you have
not specified a Main() method). Open up a command prompt and enter the following command to
ilasm.exe:
ilasm /dll CilTypes.il
After you have done so, can now open your compiled assembly into ildasm.exe to verify the
creation of each type. After you have confirmed the contents of your assembly, run peverify.exe against
it, like so:
peverify CilTypes.dll
668