CHAPTER 18 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES
Building a .NET Assembly with CIL
Now that you’ve taken a tour of the syntax and semantics of raw CIL, it’s time to solidify your current
understanding by building a .NET application using nothing but ilasm.exe and your text editor of
choice. Specifically, your application will consist of a privately deployed, single-file *.dll that contains
two class type definitions, and a console-based *.exe that interacts with these types.
Building CILCars.dll
The first order of business is to build the *.dll to be consumed by the client. Open a text editor and
create a new *.il file named CILCars.il. This single-file assembly will make use of two external .NET
assemblies. Begin by updating your code file as follows:
// Reference mscorlib.dll and
// System.Windows.Forms.dll.
.assembly extern mscorlib
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 4:0:0:0
}
.assembly extern System.Windows.Forms
{
.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )
.ver 4:0:0:0
}
// Define the single-file assembly.
.assembly CILCars
{
.hash algorithm 0x00008004
.ver 1:0:0:0
}
.module CILCars.dll
This assembly will contain two class types. The first type, CILCar, defines two points of field data
(public for simplicity in this example) and a custom constructor. The second type, CarInfoHelper,
defines a single static method named DisplayCarInfo(), which takes CILCar as a parameter and returns
void. Both types are in the CILCars namespace. In terms of CIL, CILCar can be implemented as follows:
// Implementation of CILCars.CILCar type.
.namespace CILCars
{
.class public auto ansi beforefieldinit CILCar
extends [mscorlib]System.Object
{
// The field data of the CILCar.
.field public string petName
.field public int32 currSpeed
// The custom constructor simply allows the caller
// to assign the field data.
.method public hidebysig specialname rtspecialname
679