What is IL Weaving?
Asked Answered
C

3

65

I just saw Ayende's post today about PostSharp. I downloaded the code and tried it out, and I thought it was the coolest, most easy to use way to handle AOP that I've seen.

In his post, Ayende says that PostSharp accomplishes it's magic via IL Weaving. Now, at some abstract level I can deduce what that means, but I wanted to see if there was a more detailed answer out there. Unfortunately, for the first time in a very long time, Google came up empty for me. And so I thought this would be a great question for StackOverflow (since I've been a subscribe to Jeff's blog for a couple years now and knew this site was doing its thing).

So what exactly is IL Weaving and how is it accomplished?

Cheliform answered 9/10, 2008 at 21:57 Comment(0)
I
35

Weaving refers to the process of injecting functionality into an existing program. This can be done conceptually at a number of levels:

  • Source code weaving would inject source code lines before the code is compiled
  • IL weaving (for .NET) adds the code as IL instructions in the assembly
  • ByteCode weaving (for Java) works on the class file, see these comments wrt AspectJ

In theory you could go one deeper and weave with an executable compiled to native instructions, but this would add a lot of complexity and I'm not aware of anything that does this.

Indicia answered 9/10, 2008 at 22:4 Comment(3)
Native instruction "weaving" is commonplace in native executable debuggers. On the Intel x86 instruction set, for example, setting a breakpoint in code requires inserting an INT 3 opcode into the target code. This overwrites the native instruction, so you also have to save the byte that was there and put it back after the breakpoint is hit so the program will execute normally. Yes, it's complicated, which is why writing debuggers is a rarefied art.Twinflower
some links I collected on this subject: source code weaving tara.tcd.ie/handle/2262/1239; IL weaving gutgames.com/post/Creating-an-AOP-Library-in-Net-Part-1.aspx; ByteCode weaving eclipse.org/aspectj/doc/released/devguide/…; Aspect# developerfusion.com/article/5307/…Saponify
Note that in the comment that Jeroen left, the post from gutgames.com actually talks about implementing AOP using subclassing, not IL weaving.Taryn
A
12

IL Weaving is simlar to ByteCode Weaving. .Net compiles to an intermediate language which is run by the .NET clr. IL Weaving is basically analyzing and altering the intermediate language. Since it is done at that level it can be done for all .NET languages.

Acie answered 9/10, 2008 at 22:0 Comment(0)
E
4

Might help if you Google MSIL Injection instead. This link is from PostSharp and it explains it well.

My company has recently bought a product called dynaTrace for performance diagnosis. It uses MSIL injection to instrument around methods. Basically it adds IL code before your method and after to time the method (I am sure there is a lot more to it than that).

And here is a fairly involved but good explanation of the process.

Eohippus answered 9/10, 2008 at 22:2 Comment(2)
Your second link ('here') is no longer available, probably since the corresponding issue has been archived. Do you, by accident, recall the year/month of the issue you were referring to? Thanks in advance.Danielledaniels
The link is back up. I added some info on opening it.Lurlinelusa

© 2022 - 2024 — McMap. All rights reserved.