Compile IL code at runtime using .NET 3.5 and C# from file

Posted by nitefrog on Stack Overflow See other posts from Stack Overflow or by nitefrog
Published on 2010-05-01T06:34:58Z Indexed on 2010/05/01 6:37 UTC
Read the original article Hit count: 262

Filed under:
|
|
|
|

I would like to take a file that is an IL file, and at run time compile it back to an exe.

Right now I can use process.start to fire off the command line with parameters (ilasm.exe) but I would like to automate this process from a C# service I will create.

Is there a way to do this with reflection and reflection.emit?

While this works:

string rawText = File.ReadAllText(string.Format("c:\\temp\\{0}.il", Utility.GetAppSetting("baseName")), Encoding.ASCII);

rawText = rawText.Replace("[--STRIP--]", guid);

File.Delete(string.Format("c:\\temp\\{0}.il", Utility.GetAppSetting("baseName")));

File.WriteAllText(string.Format("c:\\temp\\{0}.il", Utility.GetAppSetting("baseName")),rawText, Encoding.ASCII);

pi = new ProcessStartInfo();
pi.WindowStyle = ProcessWindowStyle.Hidden;
pi.FileName = "\"" + ilasm + "\"";
pi.Arguments = string.Format("c:\\temp\\{0}.il", Utility.GetAppSetting("baseName"));

using(Process p = Process.Start(pi))
{
    p.WaitForExit();
}

It is not ideal as I really would like this to be a streamlined process.

I have seen examples of creating the IL at runtime, then saving, but I need to use the IL I already have in file form and compile it back to an exe.

Thanks.

© Stack Overflow or respective owner

Related posts about c#

Related posts about compiler