I'm writing a compiler that targets CIL (.Net) byte code. For the part that actually generates byte code, I could write it from scratch, but it would be convenient to use an existing library if possible.
I need the ability to generate byte code both to disk (for the usual kind of static compilation) and in memory (runtime code generation). Also the ability to look up existing assemblies, at least to the extent of being able to resolve references.
As I understand it, there are three APIs that do at least something along these lines, but it's not immediately clear whether they do exactly what's needed:
Reflection. This certainly seems to be able to generate byte code in memory. Does it include a way to generate a complete assembly to disk?
Mono.Cecil. I think this would be suitable? Their update page says "For this beta we’ve worked with the Mono team to support their need while they move to Roslyn and Portable PDBs." Does this mean Cecil is now superseded by Roslyn, or that it now works with Roslyn?
Roslyn. Presumably this must have the ability to generate byte code to disk. Does it have the ability to generate byte code in memory at runtime?
Which is the recommended solution these days?
Yes, yes and yes. All of them can target both memory and disk.
All of those are alternatives with different capabilities and different purposes. If you're writing a compiler for a language of your choice, all you need is ILGenerator
. If you want to compile into e.g. C# and then into IL, use Roslyn. Cecil gives you a few extra tools, but its biggest advantage is the ability to modify (and decompile) existing code on the fly - handy for certain tools, but probably not for your use case.