MSIL是什么呢?
先看微软官方给出的c#程序运行过程
从上图可以看出,MSIL是介于c#源文件和计算机代码的一中中间过渡语言。
.Net平台MSIL编译工具(Ilasm.exe)和反编译工具(Ildasm.exe)。
我们今天来了解一下反编译工具:
我们新建控制台应用程序 MSILTest 并编写如下代码:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello world!");
Console.ReadLine();
}
}
执行输出Hello world
下来我们进行反编译: 开始→所有程序→Microsoft Visual Studio 2005→Visual Studio Tools→Visual Studio 2005命令提示
输入命令 ildasm E:\MSILTest\MSILTest\bin\Debug\MSILTest.exe
命令 ildasm代表反编译,E:\MSILTest\MSILTest\bin\Debug\MSILTest.exe是控制台程编译后生成的exe
(也可以 开始→所有程序→Microsoft .Net FrameWork SDK v2.0→Tools→MSIL反编译程序,然后选择MSILTest.exe)
要看懂先来了解每个图标代表的意思,参考微软官方IL树
可以查看更多
命名空间
类
.ver 2:0:0:0 //版本号
}
// MVID: {118CBB3B-E1A4-4453-A91E-85C22F861EDC}
.imagebase 0x00400000
.file alignment 0x00001000
.stackreserve 0x00100000
.subsystem 0x0003 // WINDOWS_CUI
.corflags 0x00000001 // ILONLY
// Image base: 0x03A50000
extends [mscorlib]System.Object
{
} // end of class MSILTest.Program
instance void .ctor() cil managed //cil managed代表托管代码
{
// 代码大小 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method Program::.ctords
{
.entrypoint
// 代码大小 19 (0x13)
.maxstack 8
IL_0000: nop
IL_0001: ldstr "hello world!"
IL_0006: call void [mscorlib]System.Console::WriteLine(string)
IL_000b: nop
IL_000c: call string [mscorlib]System.Console::ReadLine()
IL_0012: ret
} // end of method Program::Main