Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Pro CSharp 2008 And The .NET 3.5 Platform [eng]

.pdf
Скачиваний:
78
Добавлен:
16.08.2013
Размер:
22.5 Mб
Скачать

642 CHAPTER 19 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES

This innocent-looking method has a lot to say in terms of CIL. First, the incoming arguments (a and b) must be pushed onto the virtual execution stack using the ldarg (load argument) opcode. Next, the add opcode will be used to pop the next two values off the stack and find the summation, and store the value on the stack yet again. Finally, this sum is popped off the stack and returned to the caller via the ret opcode. If you were to disassemble this C# method using ildasm.exe, you would find numerous additional tokens injected by csc.exe, but the crux of the CIL code is quite simple:

.method public hidebysig static int32 Add(int32 a, int32 b) cil managed

{

.maxstack

2

ldarg.0

// Load "a" onto the stack.

ldarg.1

// Load "b" onto the stack.

add

// Add both values.

ret

 

}

The Hidden this Reference

Notice that the two incoming arguments (a and b) are referenced within the CIL code using their indexed position (index 0 and index 1), given that the virtual execution stack begins indexing at position 0.

One thing to be very mindful of when you are examining or authoring CIL code is that every nonstatic method that takes incoming arguments automatically receives an implicit additional parameter, which is a reference to the current object (think the C# this keyword). Given this, if the Add() method were defined as nonstatic:

// No longer static!

public int Add(int a, int b)

{

return a + b;

}

the incoming a and b arguments are loaded using ldarg.1 and ldarg.2 (rather than the expected ldarg.0 and ldarg.1 opcodes). Again, the reason is that slot 0 actually contains the implicit this reference. Consider the following pseudo-code:

// This is JUST pseudo-code!

.method public hidebysig static int32 AddTwoIntParams(

MyClass_HiddenThisPointer this, int32 a, int32 b) cil managed

{

 

ldarg.0

// Load MyClass_HiddenThisPointer onto the stack.

ldarg.1

// Load "a" onto the stack.

ldarg.2

// Load "b" onto the stack.

...

 

}

 

Representing Iteration Constructs in CIL

Iteration constructs in the C# programming language are represented using the for, foreach, while, and do keywords, each of which has a specific representation in CIL. Consider the classic for loop:

CHAPTER 19 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES

643

public static void CountToTen()

{

for(int i = 0; i < 10; i++)

;

}

Now, as you may recall, the br opcodes (br, blt, and so on) are used to control a break in flow when some condition has been met. In this example, you have set up a condition in which the for loop should break out of its cycle when the local variable i is equal to or greater than the value of 10. With each pass, the value of 1 is added to i, at which point the test condition is yet again evaluated.

Also recall that when you make use of any of the CIL branching opcodes, you will need to define a specific code label (or two) that marks the location to jump to when the condition is indeed true. Given these points, ponder the following (augmented) CIL code generated via ildasm.exe (including the autogenerated code labels):

.method public hidebysig static void CountToTen() cil managed

{

.maxstack

2

 

 

.locals init ([0] int32 i) // Init the local integer "i".

IL_0000:

ldc.i4.0

 

// Load this value onto the stack.

IL_0001:

stloc.0

 

// Store this value at index "0".

IL_0002:

br.s IL_0008

// Jump to IL_0008.

IL_0004:

ldloc.0

 

// Load value of variable at index 0.

IL_0005:

ldc.i4.1

 

// Load the value "1" on the stack.

IL_0006:

add

 

// Add current value on the stack at index 0.

IL_0007:

stloc.0

 

 

IL_0008:

ldloc.0

 

// Load value at index "0".

IL_0009:

ldc.i4.s

10

// Load value of "10" onto the stack.

IL_000b: blt.s IL_0004

// Less than? If so, jump back to IL_0004

IL_000d:

ret

 

 

}

In a nutshell, this CIL code begins by defining the local int32 and loading it onto the stack. At this point, you jump back and forth between code label IL_0008 and IL_0004, each time bumping the value of i by 1 and testing to see whether i is still less than the value 10. If so, you exit the method.

Source Code The CilTypes example is included under the Chapter 19 subdirectory.

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 binaries. Begin by updating your code file as follows:

644CHAPTER 19 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES

//Reference mscorlib.dll and

//System.Windows.Forms.dll

.assembly extern mscorlib

{

.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )

.ver 2:0:0:0

}

.assembly extern System.Windows.Forms

{

.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )

.ver 2:0:0:0

}

// Define the single-file assembly.

.assembly CILCars

{

.hash algorithm 0x00008004

.ver 1:0:0:0

}

.module CILCars.dll

As mentioned, this assembly will contain two class types. The first type, CILCar, defines two points of field data 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 instance void .ctor(int32 c, string p) cil managed

{

.maxstack 8

//Load first arg onto the stack and call base class ctor. ldarg.0 // "this" object, not the int32!

call instance void [mscorlib]System.Object::.ctor()

//Now load first and second args onto the stack.

ldarg.0 // "this" object ldarg.1 // int32 arg

//Store topmost stack (int 32) member in currSpeed field. stfld int32 CILCars.CILCar::currSpeed

//Load string arg and store in petName field.

ldarg.0 // "this" object ldarg.2 // string arg

CHAPTER 19 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES

645

stfld string CILCars.CILCar::petName ret

}

}

}

Keeping in mind that the real first argument for any nonstatic member is the current object reference, the first block of CIL simply loads the object reference and calls the base class constructor. Next, you push the incoming constructor arguments onto the stack and store them into the type’s field data using the stfld (store in field) opcode.

Now let’s implement the second type in this namespace: CILCarInfo. The meat of the type is found within the static Display() method. In a nutshell, the role of this method is to take the incoming CILCar parameter, extract the values of its field data, and display it in a Windows Forms message box. Here is the complete implementation of CILCarInfo, with analysis to follow:

.class public auto ansi beforefieldinit CILCarInfo extends [mscorlib]System.Object

{

.method public hidebysig static void Display(class CILCars.CILCar c) cil managed

{

.maxstack 8

//We need a local string variable.

.locals init ([0] string caption)

//Load string and the incoming CILCar onto the stack. ldstr "{0}'s speed is:"

ldarg.0

//Now place the value of the CILCar's petName on the

//stack and call the static String.Format() method. ldfld string CILCars.CILCar::petName

call string [mscorlib]System.String::Format(string, object) stloc.0

//Now load the value of the currSpeed field and get its string

//representation (note call to ToString() ).

ldarg.0

ldflda int32 CILCars.CILCar::currSpeed

call instance string [mscorlib]System.Int32::ToString() ldloc.0

// Now call the MessageBox.Show() method with loaded values. call valuetype [System.Windows.Forms]

System.Windows.Forms.DialogResult

[System.Windows.Forms] System.Windows.Forms.MessageBox::Show(string, string)

pop ret

}

}

Although the amount of CIL code is a bit more than you see in the implementation of CILCar, things are still rather straightforward. First, given that you are defining a static method, you don’t have to be concerned with the hidden object reference (thus, the ldarg.0 opcode really does load the incoming CILCar argument).

646 CHAPTER 19 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES

The method begins by loading a string ("{0}'s speed is") onto the stack, followed by the CILCar argument. Once these two values are in place, you load the value of the petName field and call the static System.String.Format() method to substitute the curly bracket placeholder with the CILCar’s pet name.

The same general procedure takes place when processing the currSpeed field, but note that you use the ldflda opcode, which loads the argument address onto the stack. At this point, you call System.Int32.ToString() to transform the value at said address into a string type. Finally, once both strings have been formatted as necessary, you call the MessageBox.Show() method.

At this point, you are able to compile your new *.dll using ilasm.exe with the following command:

ilasm /dll CILCars.il

and verify the contained CIL using peverify.exe:

peverify CILCars.dll

Building CILCarClient.exe

Now you can build a simple *.exe assembly that will

Make a CILCar type.

Pass the type into the static CILCarInfo.Display() method.

Create a new file named CarClient.il and define external references to mscorlib.dll and CILCars.dll (don’t forget to place a copy of this .NET assembly in the client’s application directory!). Next, define a single type (Program) that manipulates the CILCars.dll assembly. Here’s the complete code:

//External assembly refs.

.assembly extern mscorlib

{

.publickeytoken = (B7 7A 5C 56 19 34 E0 89 )

.ver 2:0:0:0

}

.assembly extern CILCars

{

.ver 1:0:0:0

}

//Our executable assembly.

.assembly CarClient

{

.hash algorithm 0x00008004

.ver 0:0:0:0

}

.module CarClient.exe

// Implementation of Program type

.namespace CarClient

{

.class private auto ansi beforefieldinit Program extends [mscorlib]System.Object

{

.method private hidebysig static void

CHAPTER 19 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES

647

Main(string[] args) cil managed

{

//Marks the entry point of the *.exe.

.entrypoint

.maxstack 8

//Declare a local CILCar type and push

//values on the stack for ctor call.

.locals init ([0] class [CILCars]CILCars.CILCar myCilCar) ldc.i4 55

ldstr "Junior"

//Make new CilCar; store and load reference. newobj instance void

[CILCars]CILCars.CILCar::.ctor(int32, string) stloc.0

ldloc.0

//Call Display() and pass in topmost value on stack. call void [CILCars]

CILCars.CILCarInfo::Display( class [CILCars]CILCars.CILCar)

ret

}

}

}

The one opcode that is important to point out is .entrypoint. Recall from the discussion earlier in this chapter that this opcode is used to mark which method of an *.exe functions as the entry point of the module. In fact, given that .entrypoint is how the CLR identifies the initial method to execute, this method can be called anything, although here we are using the standard method name of Main(). The remainder of the CIL code found in the Main() method is your basic pushing and popping of stack-based values.

Do note, however, that the creation of CILCar involves the use of the .newobj opcode. On a related note, recall that when you wish to invoke a member of a type using raw CIL, you make use of the double-colon syntax and, as always, make use of the fully qualified name of the type. With this, you can compile your new file with ilasm.exe, verify your assembly with peverify.exe, and execute your program. Issue the following commands within your command prompt:

ilasm CarClient.il peverify CarClient.exe CarClient.exe

Figure 19-7 shows the end result.

Figure 19-7. Your CILCar in action

648 CHAPTER 19 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES

That wraps up the CIL primer and the first goal of this chapter. At this point, I hope you feel confident that you can open a particular .NET assembly using ildasm.exe (or a similar tool) and gain a better understanding of what exactly is occurring behind the scenes.

Source Code The CilCars example is included under the Chapter 19 subdirectory.

Understanding Dynamic Assemblies

As you may have gathered, the process of building a complex .NET application in CIL would be quite the labor of love. On the one hand, CIL is an extremely expressive programming language that allows you to interact with all of the programming constructs allowed by the CTS. On the other hand, authoring raw CIL is tedious, error-prone, and painful. While it is true that knowledge is power, you may indeed wonder just how important it is to commit the laws of CIL syntax to memory. The answer is, “It depends.” To be sure, most of your .NET programming endeavors will not require you to view, edit, or author CIL code. However, with the CIL primer behind you, you are now ready to investigate the world of dynamic assemblies (as opposed to static assemblies) and the role of the System.Reflection.Emit namespace.

The first question you may have is, “What exactly is the difference between static and dynamic assemblies?” By definition, static assemblies are .NET binaries loaded directly from disk storage, meaning they are located somewhere on your hard drive in a physical file (or possibly a set of files in the case of a multifile assembly) at the time the CLR requests them. As you might guess, every time you compile your C# source code, you end up with a static assembly.

A dynamic assembly, on the other hand, is created in memory on the fly using the types provided by the System.Reflection.Emit namespace. The System.Reflection.Emit namespace makes it possible to create an assembly and its modules, type definitions, and CIL implementation logic at runtime. Once you have done so, you are then free to save your in-memory binary to disk. This, of course, results in a new static assembly. To be sure, the process of building a dynamic assembly using the System.Reflection.Emit namespace does require some level of understanding regarding the nature of CIL opcodes.

Although creating dynamic assemblies is a fairly advanced (and uncommon) programming task, they can be useful under various circumstances:

You are building a .NET programming tool that needs to generate assemblies on demand based on user input.

You are building a program that needs to generate proxies to remote types on the fly based on the obtained metadata.

You wish to load a static assembly and dynamically insert new types into the binary image.

Several aspects of the .NET runtime engine involve generating dynamic assemblies quietly in the background. For example, ASP.NET makes use of this technique to map markup and server-side script code into a runtime object model. LINQ also can generate code on the fly based on various query expressions. This being said, let’s check out the types within System.Reflection.Emit.

Exploring the System.Reflection.Emit Namespace

Creating a dynamic assembly requires you to have some familiarity with CIL opcodes, but the types of the System.Reflection.Emit namespace hide the complexity of CIL as much as possible. For

CHAPTER 19 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES

649

example, rather than directly specifying the necessary CIL directives and attributes to define a class type, you can simply make use of the TypeBuilder class. Likewise, if you wish to define a new instance-level constructor, you have no need to emit the specialname, rtspecialname, or .ctor tokens; rather, you can make use of the ConstructorBuilder. Table 19-8 documents the key members of the System.Reflection.Emit namespace.

Table 19-8. Select Members of the System.Reflection.Emit Namespace

Members

Meaning in Life

AssemblyBuilder

Used to create an assembly (*.dll or *.exe) at runtime. *.exes must

 

call the ModuleBuilder.SetEntryPoint() method to set the method

 

that is the entry point to the module. If no entry point is specified, a

 

*.dll will be generated.

ModuleBuilder

Used to define the set of modules within the current assembly.

EnumBuilder

Used to create a .NET enumeration type.

TypeBuilder

May be used to create classes, interfaces, structures, and delegates

 

within a module at runtime.

MethodBuilder

Used to create type members (such as methods, local variables,

LocalBuilder

properties, constructors, and attributes) at runtime.

PropertyBuilder

 

FieldBuilder

 

ConstructorBuilder

 

CustomAttributeBuilder

 

ParameterBuilder

 

EventBuilder

 

ILGenerator

Emits CIL opcodes into a given type member.

OpCodes

Provides numerous fields that map to CIL opcodes. This type is used

 

in conjunction with the various members of System.Reflection.

 

Emit.ILGenerator.

 

 

In general, the types of the System.Reflection.Emit namespace allow you to represent raw CIL tokens programmatically during the construction of your dynamic assembly. You will see many of these members in the example that follows; however, the ILGenerator type is worth checking out straightaway.

The Role of the System.Reflection.Emit.ILGenerator

As its name implies, the ILGenerator type’s role is to inject CIL opcodes into a given type member. However, you cannot directly create ILGenerator objects, as this type has no public constructors, rather you receive an ILGenerator type by calling specific methods of the builder-centric types (such as the MethodBuilder and ConstructorBuilder types), for example:

//Obtain an ILGenerator from a ConstructorBuilder

//object named "myCtorBuilder".

ConstructorBuilder myCtorBuilder =

new ConstructorBuilder(/* ...various args... */); ILGenerator myCILGen = myCtorBuilder.GetILGenerator();

Once you have an ILGenerator in your hands, you are then able to emit the raw CIL opcodes using any number of methods. Table 19-9 documents some (but not all) methods of ILGenerator.

650 CHAPTER 19 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES

Table 19-9. Various Methods of ILGenerator

Method

Meaning in Life

BeginCatchBlock()

Begins a catch block

BeginExceptionBlock()

Begins an exception block for a nonfiltered exception

BeginFinallyBlock()

Begins a finally block

BeginScope()

Begins a lexical scope

DeclareLocal()

Declares a local variable

DefineLabel()

Declares a new label

Emit()

Is overloaded numerous times to allow you to emit CIL opcodes

EmitCall()

Pushes a call or callvirt opcode into the CIL stream

EmitWriteLine()

Emits a call to Console.WriteLine() with different types of values

EndExceptionBlock()

Ends an exception block

EndScope()

Ends a lexical scope

ThrowException()

Emits an instruction to throw an exception

UsingNamespace()

Specifies the namespace to be used in evaluating locals and watches for

 

the current active lexical scope

 

 

The key method of ILGenerator is Emit(), which works in conjunction with the System. Reflection.Emit.OpCodes class type. As mentioned earlier in this chapter, this type exposes a good number of read-only fields that map to raw CIL opcodes. The full set of these members are all documented within online help, and you will see various examples in the pages that follow.

Emitting a Dynamic Assembly

To illustrate the process of defining a .NET assembly at runtime, let’s walk through the process of creating a single-file dynamic assembly named MyAssembly.dll. Within this module is a class named HelloWorld. The HelloWorld type supports a default constructor and a custom constructor that is used to assign the value of a private member variable (theMessage) of type string. In addition, HelloWorld supports a public instance method named SayHello(), which prints a greeting to the standard I/O stream, and another instance method named GetMsg(), which returns the internal private string. In effect, you are going to programmatically generate the following class type:

//This class will be created at runtime

//using System.Reflection.Emit.

public class HelloWorld

{

private string theMessage; HelloWorld() {}

HelloWorld(string s) {theMessage = s;}

public string GetMsg() {return theMessage;} public void SayHello()

{

System.Console.WriteLine("Hello from the HelloWorld class!");

}

}

Assume you have created a new Visual Studio 2008 Console Application project workspace named DynamicAsmBuilder and import the System.Reflection, System.Reflection.Emit, and

CHAPTER 19 UNDERSTANDING CIL AND THE ROLE OF DYNAMIC ASSEMBLIES

651

System.Threading namespaces. Define a static method named CreateMyAsm(). This single method is in charge of the following:

Defining the characteristics of the dynamic assembly (name, version, etc.)

Implementing the HelloClass type

Saving the in-memory assembly to a physical file

Also note that the CreateMyAsm() method takes as a single parameter a System.AppDomain type, which will be used to obtain access to the AssemblyBuilder type associated with the current application domain (see Chapter 17 for a discussion of .NET application domains). Here is the complete code, with analysis to follow:

// The caller sends in an AppDomain type.

public static void CreateMyAsm(AppDomain curAppDomain)

{

//Establish general assembly characteristics.

AssemblyName assemblyName = new AssemblyName(); assemblyName.Name = "MyAssembly"; assemblyName.Version = new Version("1.0.0.0");

//Create new assembly within the current AppDomain.

AssemblyBuilder assembly = curAppDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save);

//Given that we are building a single-file

//assembly, the name of the module is the same as the assembly.

ModuleBuilder module = assembly.DefineDynamicModule("MyAssembly", "MyAssembly.dll");

//Define a public class named "HelloWorld".

TypeBuilder helloWorldClass = module.DefineType("MyAssembly.HelloWorld",

TypeAttributes.Public);

//Define a private String member variable named "theMessage".

FieldBuilder msgField =

helloWorldClass.DefineField("theMessage", Type.GetType("System.String"), FieldAttributes.Private);

//Create the custom ctor.

Type[] constructorArgs = new Type[1]; constructorArgs[0] = typeof(string); ConstructorBuilder constructor =

helloWorldClass.DefineConstructor(MethodAttributes.Public,

CallingConventions.Standard,

constructorArgs);

ILGenerator constructorIL = constructor.GetILGenerator(); constructorIL.Emit(OpCodes.Ldarg_0);

Type objectClass = typeof(object); ConstructorInfo superConstructor =

objectClass.GetConstructor(new Type[0]); constructorIL.Emit(OpCodes.Call, superConstructor); constructorIL.Emit(OpCodes.Ldarg_0); constructorIL.Emit(OpCodes.Ldarg_1); constructorIL.Emit(OpCodes.Stfld, msgField); constructorIL.Emit(OpCodes.Ret);