I wanted to write another small sample to demonstrate how you can use ilasm to modify a compiled assembly. The sample is very simple, but demonstrates the concept well. Let us take App.cs which contains the following code:
public class sample
Console.WriteLine("Inside main ...");
Let us compile App.cs to App.exe. This executable now prints Inside main … when it is run.
Now let us attempt to change the output of this executable without touching its source files.
You run ildasm App.exe /out:App.il to generate the IL code corresponding to the executable. You can now edit this il code in notepad and change the ldstr from “Inside main …” to “Inside main changed …” as shown below
IL_0001: ldstr "Inside main ..."
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_0001: ldstr "Inside main changed ..."
IL_0006: call void mscorlib]System.Console::WriteLine(string)
Now let us rebuild the executable from this il file by running ilasm App.il. This generates a new App.exe. If you run App.exe you will get the following output “Inside main changed …”