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?
System.Reflection.Emit
is fine for production compilers, though you may want to take a look at mono-cecil.
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:
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.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With