0

I am trying to write an MSBuild task that lets me inline IL code into methods, similar to Glenn Slayden's answer to this question. I have written code to decompile and modify the IL, but I don't know how to recompile it.
No libraries that I have found, including Mono.Cecil, allows you to compile raw IL code from a single string. Glenn Slayden uses ILAsm, but I can't find ILAsm for .NET 5.

How do I compile raw IL code from a single string?

  • 1
    You could probably use [ilasm.exe](https://learn.microsoft.com/en-us/dotnet/framework/tools/ilasm-exe-il-assembler) to compile IL that has been saved to a file, then just use `Assembly.Load(File.ReadAllBytes("path"))` to load it. It's hard to tell if this would be a valid approach since you don't include your implementation on generating the IL. – DekuDesu Jul 24 '21 at 06:33
  • Does this answer your question? [Compiling code dynamically using C#](https://stackoverflow.com/questions/4718329/compiling-code-dynamically-using-c-sharp) –  Jul 24 '21 at 08:58
  • No, that does not answer my question. I have a string of IL code that I want to compile. @DekuDesu I can't use ILAsm because of the lack of an implementation for .NET 5. If there is one, can you please send it or an article relating to it to me? Something like ILAsm seems like my only option at the moment. – Wannabe Programmer Jul 24 '21 at 13:25

2 Answers2

1

I found the answer to my problem. 3F, the guy who made DllExport, has a modified version of CoreCLR that comes with ILAsm and ILDasm.

1

Microsoft now publishes ILAsm and ILDasm on NuGet, under the following IDs:

Microsoft.NETCore.ILAsm
Microsoft.NETCore.ILDAsm

You can install them into your current directory with nuget install {ID}, then run them with {ID}.{version}\runtimes\native\{exe name}.exe.

Joe Sewell
  • 6,067
  • 1
  • 21
  • 34