Why would you need to emit IL code? [closed]
Asked Answered
R

2

7

I work in a code base that is quite large and today I found a project that was emitting IL code inside a normal class.

The project containing the IL code being emitted was a implementation of a Service Locator MSDN Desctiption.

What are the advantages of doing this and why would this be done as apposed to using the C# language?

Recreate answered 30/7, 2013 at 12:12 Comment(4)
Depends... What was the IL code doing?Rarely
My understanding of IL in extremely limited, so do not know.Recreate
And that is one of the biggest problem with emitting IL. It requires a lot of knowledge the normal programmer doesn't have, just to read the code afterwards, and even if you do have that knowledge, it's still hard to read.Pouch
If you want to write IL, the clear path is SIGIL github.com/kevin-montrose/Sigil which was created to be used by Stackoverflow and it's related toolsPrecondemn
P
15

Typically this is done to circumvent the overhead of using reflection, using information only available at runtime.

You would then use reflection, which can be slow depending on what you do, to build a new piece of code that works directly with the data given to it, without using reflection.

Advantages:

  • Performance

Disadvantages:

  • Hard to debug
  • Hard to get right
  • Hard to read code afterwards
  • Steep learning curve

So you need to ensure it's really worth the price before embarking on this.

Note that this is a general answer. In the specific case you came across, there is no way to answer why this was done nor which particular advantages (or disadvantages) you would have without actually seeing the code.

Pouch answered 30/7, 2013 at 12:15 Comment(5)
It was in an implementation of a Service Locator so that would be the reflection part I beleiveRecreate
Probably. I've done that myself, implemented an IoC container that emits IL to construct the object graphs. Framework code, may be OK, application-level code, probably not.Pouch
You can often get performance without many of the other problems by using expression trees. For example, the ObjectModelAdaptor.BuildAccessor methods all generate code at runtime, but none of them explicitly construct the IL opcode sequences. This strategy also completely eliminated bugs we hit during initial testing due to differences in handling between reference types and value types.Yentai
So for an enterprise level application using a service locator pattern the cost of reflection can be reduced but you obviously have to carry the disadvantages along with it.Recreate
This was the exact answer I was after @LasseV.KarlsenRecreate
G
5

There are many uses for this.
One of the more often used scenario is for changing/injecting code on the fly:
.NET CLR Injection: Modify IL Code during Run-time


A good tutorial that help me to understand a good use for it is: Dynamic... But Fast: The Tale of Three Monkeys, A Wolf and the DynamicMethod and ILGenerator Classes


Good luck

Gilbert answered 30/7, 2013 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.