5

I’m writing a small compiler in C# and planning to generate IL instructions for .Net platform using System.Reflection.Emit. My question is, it is advisable to use System.Reflection.Emit for generating IL for production compilers.

If it is not advisable to use System.Reflection.Emit for generating IL for production compilers, do we have alternative libraries/tools for that purpose?

Upul Bandara
  • 5,973
  • 4
  • 37
  • 60

4 Answers4

7

System.Reflection.Emit is fine for production compilers, though you may want to take a look at mono-cecil.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • 1
    +1. Microsoft.CCI is also an option. It seems like performance is a strong consideration in that library (explicit name tables being one example), so it might allow you to make a faster compiler. But in my limited use, I much preferred Cecil's API. – Merlyn Morgan-Graham Jun 01 '11 at 05:51
2

Here is a blog article describing some problems with Reflection.Emit that probably aren't serious limitations for your project but you can be the judge:

If those issues don't bother you, then you can use this SO question for tips on the generation using Reflection.Emit and writing the assembly to disk:

Community
  • 1
  • 1
Rick Sladkey
  • 33,988
  • 6
  • 71
  • 95
2

While System.Reflection.Emit let's you do code gen it's the oldest of the code gen APIs in the .NET framework and it requires understanding of IL. The expression trees introduced in .NET 3.5 and extended in .NET 4.0 (can't create new types) but they can be used to assemble complete method bodies.

John Leidegren
  • 59,920
  • 20
  • 131
  • 152
1

Reflection.Emit is generally fine, but IKVM.Reflection.Emit (available from Mono) may have more options if the assembly you are generating is not for the same platform (i.e. you want to build a silverlight dll from your compiler, which is written in "full" .NET).

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    mono is awesome in every way. They build lots of cool stuff everybody .NET developer should be aware of. I did not know about `IKVM`. – John Leidegren Jun 01 '11 at 07:19