2. Why learn CIL? Understand how various .net languages map to CIL tokens. Disassemble, edit CIL, and recompile. Build dynamic assemblies using System.Refelection.Emit. Leveraging CTS features that aren’t available in higher level languages (C# can’t define global level members for example).
4. Opcode/Mnemonic Distinction Mnemonic symbols (add, sub) are used rather than binary opcodes (0X85, 0X59). Decompilers like ildasm.exe map the opcodes to their mnemonic representations.
5. CIL is Stack Based When looking at CIL code you’ll notice things like stloc.0 – the zero represents index 0 on the stack.
6. Look at Compiled Assemblies Use ildsam.exe to disassemble assembly. <Microsoft SDKs>indows7.0Ain Commercial tools exist too: Reflector http://reflector.red-gate.com
7. Write IL Code with an IDE MonoDevelop http://monodevelop.com
11. Generate Dynamic Assemblies We’ll use the System.Reflection.Emitnamespace to output dynamic code into our current AppDomain.
12. public static void CreateMyAsm(AppDomaincurAppDomain) { // Build the assembly name. var name = new AssemblyName("MyAssembly") {Version = new Version("1.0.0.0")}; // Create a new assembly in the current app domain. var assembly = curAppDomain.DefineDynamicAssembly(name, AssemblyBuilderAccess.Save); // Because this is a single file assembly, the module name is the same as assembly. var module = assembly.DefineDynamicModule("MyAssembly", "MyAssembly.dll"); // Define the public class named "HelloWorld". varhelloWorldClass = module.DefineType("MyAssembly.HelloWorld", TypeAttributes.Public); // Define the string variable named "theMessage". varmsgField = helloWorldClass.DefineField("theMessage", Type.GetType("System.String"), FieldAttributes.Private); // Create the custom constructor. varconstructorArgs = new[] {typeof (string)}; var constructor = helloWorldClass.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, constructorArgs); varconstructorIL = constructor.GetILGenerator(); constructorIL.Emit(OpCodes.Ldarg_0); varobjectClass = typeof (object); varsuperConstructor = 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); // Create the default constructor. helloWorldClass.DefineDefaultConstructor(MethodAttributes.Public); // create the GetMsg() method. vargetMsgMethod = helloWorldClass.DefineMethod("GetMsg", MethodAttributes.Public, typeof (string), null); varmethodIL = getMsgMethod.GetILGenerator(); methodIL.Emit(OpCodes.Ldarg_0); methodIL.Emit(OpCodes.Ldfld, msgField); methodIL.Emit(OpCodes.Ret); // create the hellow world method varsayHiMethod = helloWorldClass.DefineMethod("SayHello", MethodAttributes.Public, null, null); methodIL = sayHiMethod.GetILGenerator(); methodIL.EmitWriteLine("Hello from the HelloWorld class!"); methodIL.Emit(OpCodes.Ret); // Bake the HelloWorld class helloWorldClass.CreateType(); // (optionally) save assembly to file assembly.Save("MyAssembly.dll"); }