CIL Programming:
Under the Hood of .NET [aPress]
Jason Bock

Review By Peter A. Bromberg, Ph.D.
Printer - Friendly Version

Peter Bromberg

CIL (or Common Intermediate Language) is the real "official language" of the .NET platform. It's what your VB, C#,COBOL, Perl or other .NET compliant compiler generates in your assemblies when you hit the "Build" (or Ctrl-Shift B) button.  It's what the Common Language Runtime JITs into executable code at runtime. And most importantly, it's the "glue" that holds the entire Common Language Infrastructure together. Kind of, well - like the "assembler of .NET".

Jason Bock is one of those types who is fascinated by programming languages. If you've ever read any of his articles then you'll know that this includes many languages. So, it makes a lot of sense that Bock should write a book about CIL.

Of course, your first question probably is , "What do I need to learn CIL for? I'm having a bad enough time just getting to the core competency level in [fill in your favorite higher level language here]?" The answer, in my opinion, is twofold:

1) You will become a better higher-level language programmer if you understand more of what is happening under the hood.. This can be particularly true for VB.NET programmers, who are so used to getting away with "stuff" from VB 6.0 and its predecessors that they haven't a clue what the performance impact may be when it gets compiled into CIL vs. say the same code written in C#, a language where there isn't even a concept of a choice such as "Option Strict" or "Option Explicit".

2) You'll be able to impress your friends and colleagues (no really, what you will be able to do is one of the most powerful advanced features of the .NET platform, which is to use the Emitter classes to dynamically create, compile and run code).

There are several books on the market that get into CIL a little bit and have a chapter on Reflection.Emit. But if this subject is of interest to you, I can advise you with confidence that Bock's book (has a nice ring to it, doesn't it?) is the only one that goes into the subject in depth, covering all the directives, opcodes, syntax and even a chapter on debugging with excellent instructions on creating debug symbol files, using cordbg.exe and dbgclr.exe, looking at register values and native instructions, setting breakpoints and so on.

Bock completes his book with the all - important two final chapters that tie everything together: Chapter 7 , "Emitting Types" and Chapter 8, "Dynamic Proxies in .NET".

Chapter 7 is a highly detailed explanation of the Emitter classes and how you can use your CIL knowledge to get everything out of them. You'll learn how to build and debug dynamic assemblies and types. In Chapter 8, Bock brings us to breakfast on all we've learned in previous chapters to create a dynamic type that enables you to intercept method invocations. Here is where you get some real practice on how to set up method hooks, configure the method context object, and even do type inspection.

I doubt there are any books similar to this out there. Jason Bock has put together a well - documented explanatory text on CIL and how to use it. I recommend this book for intermediate to advanced level programmers who really want to know what goes on "Under the Hood of .NET", and any programmer who needs to know the ins and outs of Reflection.Emit to generate types and assemblies.

Peter Bromberg is a C# MVP, MCP, and .NET consultant who has worked in the banking and financial industry for 20 years. He has architected and developed web - based corporate distributed application solutions since 1995, and focuses exclusively on the .NET Platform.