tags:

views:

394

answers:

5

What are the advantages of understanding CIL?

Do you have to know assembly to understand it? The code in these files looks similar.

How do I learn more about it? Any books about it like Jon Skeet's C# book?

A: 

There's probably something you could do with CIL, but I can't think of a practical example off the top of my head. In any case, here's a tutorial:

http://weblogs.asp.net/kennykerr/archive/2004/09/07/introduction-to-msil-part-1-hello-world.aspx

notnot
+2  A: 

You don't have to know assembly to learn it - it is completely separate from native assembly. Although at runtime, the CLR will basically turn it in to native assembly.

A couple possible advantages:

1) you can look at the CIL that is output by the compiler and see specifically what your code is doing

2) to solve certain kinds of problems, you may at some point want to dynamically generate code....for example, since Microsoft's policy injector framework is not quite up to snuff yet and we had need of that type of behavior we wrote some low-level, framework type plumbing..

+4  A: 

I found these books useful when learning IL and about the CLR:

Just reading through them will give you lots of information.

(I've also heard CLR via C# is really good, but haven't read it myself.)

Understanding IL and the CLR will give you a leg up when you run into a problem. (A leaky abstraction?) Although, these days with nice decompilers like Reflector, I don't find myself using ildasm as much.

Understanding assembly isn't necesarily required, but it would help. IL is actually pretty simple for the most part.

One example of where IL came in handy was determining exactly how exactly F# compiles certain constructs. Another is being able to use DynamicMethod and System.Reflection.Emit, which can open up some nice solutions in certain situations.

MichaelGG
+4  A: 

Knowledge of CIL allows you to get more out of .NET framework in some edge situations.

Note, that you do not have to be able to read raw opcodes to benefit from this knowledge. Generic knowledge of IL and assemblies, coupled with Mono.Cecil.dll can do miracles:

PS: for me the best source way of getting some practical IL knowledge was reading C# code for some Gendarme rules (it is an analogue of FxCop that uses Mono.Cecil to do the code introspection). Additionally, this article was a good start: Parsing the IL of a Method Body.

Rinat Abdullin
+2  A: 

CIL is a lot simpler than regular assembler imo. For a large part this is due to the fact that the virtual machine is stack based, so everything goes to the stack and there are no registers to keep track of.

Also there are no offset calculations, no direct memory manipulation, method calls are listed using their .NET names and so forth. I would guess that if you have a little experience with any other kind of assembly language, you will probably be able to pick up the gist of CIL code pretty quickly.

Personally I find it very useful to be able to fire up ILDasm or Reflector to look at what IL the C# compiler actually produces for some piece of code. For many of the newer features such as anonymous delegates / lambdas, captured variables, iterators and the like inspecting the IL can be very helpful.

Others have mentioned good books and I'll just throw my vote for Expert .NET 2.0 IL assembler by Serge Lidin. It has all the gory details. If you want something a little closer to C# Jeffrey Ricther's CLR via C# is excellent. Jon Skeet's book is great, but it doesn't really cover CIL.

Brian Rasmussen