I am very curios regarding MSIL work. As far as i understand in compile time CLR translates its code to the MSIL in order to provide some optimizations which is later translated by JIT to the machine code.
For example if we have :
using (TextWriter w = File.CreateText("log.txt"))
{
w.WriteLine("This is line one");
}
it will translated in MSIL to the following code:
TextWriter w = File.CreateText("log.txt");
try
{
w.WriteLine("This is line one");
}
finally
{
bool flag = w == null;
if (!flag)
{
w.Dispose();
}
}
I am not sure if it will translated exactly like that but definitely to something similar.
The point is, that i trying to investigate MSIL optimizations. Till this point i have found that MSIL translated to asm code which is not so readable for me.
Is there any tool that shows me translated MSIL in C# code or even in pseudo code ?
Highly appreciate references and related links.
P.S.
I wrote using we see above:
using (TextWriter w = File.CreateText("log.txt"))
{
w.WriteLine("This is line one");
}
and compiled it. Then i used Reflector to see the code and i saw the same code i wrote instead to see some optimizations.
The i used LINQPad with the same code. I have found that MSIL output was :
IL_0001: ldstr "log.txt"
IL_0006: call System.IO.File.CreateText
IL_000B: stloc.0 // w
IL_000C: nop
IL_000D: ldloc.0 // w
IL_000E: ldstr "This is line one"
IL_0013: callvirt System.IO.TextWriter.WriteLine
IL_0018: nop
IL_0019: nop
IL_001A: leave.s IL_002C
IL_001C: ldloc.0 // w
IL_001D: ldnull
IL_001E: ceq
IL_0020: stloc.1 // CS$4$0000
IL_0021: ldloc.1 // CS$4$0000
IL_0022: brtrue.s IL_002B
IL_0024: ldloc.0 // w
IL_0025: callvirt System.IDisposable.Dispose
IL_002A: nop
IL_002B: endfinally
From this code we see System.IDisposable.Dispose which looks like part of optimization.My intent is to see somehow C# code or pseudo code.