Generating IL for .Net Platform
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 t开发者_JAVA技巧o 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:
- The limitations of Reflection.Emit
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:
- Writing a Compiler for .net - IL or Bytecode?
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).
精彩评论