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?

ojhawkins
  • 3,200
  • 15
  • 50
  • 67
  • 1
    Depends... What was the IL code doing? – Belogix Jul 30 '13 at 12:14
  • My understanding of IL in extremely limited, so do not know. – ojhawkins Jul 30 '13 at 12:15
  • 3
    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. – Lasse V. Karlsen Jul 30 '13 at 12:16
  • 1
    If you want to write IL, the clear path is SIGIL https://github.com/kevin-montrose/Sigil which was created to be used by Stackoverflow and it's related tools – Chris Marisic May 05 '16 at 20:32

2 Answers2

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.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
  • It was in an implementation of a Service Locator so that would be the reflection part I beleive – ojhawkins Jul 30 '13 at 12:16
  • 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. – Lasse V. Karlsen Jul 30 '13 at 12:17
  • 3
    You can often get performance without many of the other problems by using expression trees. For example, the [`ObjectModelAdaptor.BuildAccessor`](https://github.com/antlr/antlrcs/blob/master/Antlr4.StringTemplate/Misc/ObjectModelAdaptor.cs) 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. – Sam Harwell Jul 30 '13 at 12:24
  • 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. – ojhawkins Jul 30 '13 at 12:26
  • This was the exact answer I was after @LasseV.Karlsen – ojhawkins Jul 31 '13 at 00:22
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

Avi Malka
  • 51
  • 1