mercredi 13 avril 2016

C# Reflection.Emit generate EXE containing copy of anonymous method

From a program that contains the following code, I want to take the code behind the Action and insert it into an EXE.

string message = "I was here.";
Action action = () =>
{
     Console.WriteLine(message);
     System.IO.File.AppendAllText(@"C:\Temp\log.txt", String.Format("{0}\r\n", message));
};

The EXE should looks something like this:

static void Main(string[] args)
{
     AnonymousMethodName();
}

static void AnonymousMethodName()
{
     Console.WriteLine("I was here");
     System.IO.File.AppendAllText(@"C:\Temp\log.txt", String.Format("{0}\r\n", "I was here"));
}

I found the following solution that gets me most of the way there (creating EXE, Main method), but I don't know IL well enough to finish it.

http://ift.tt/1S7GYAk





Aucun commentaire:

Enregistrer un commentaire