class DllTestClientApp
{
public static void Main()
{
int iage = 40;
object objage = iage;
int iold = (int)objage;
}
}
生成的IL代码如下:
.method public hidebysig static void Main () cil managed
{
.entrypoint
// 代码大小 19 (0x13)
.maxstack 1
.locals init (int32 V_0,
object V_1,
int32 V_2) //参数列表, 分别对应变量iage, objage, iold
IL_0000: ldc.i4.s 40 //将40压入堆栈
IL_0002: stloc.0 //弹出堆栈内容给第一个参数, 即V_0(iage)
IL_0003: ldloc.0 //将第一个参数压入堆栈(生成副本,性能损失)
IL_0004: box [mscorlib]System.Int32 //装箱:分配对象内存à赋值à地址压入堆栈
IL_0009: stloc.1 //弹出堆栈内容给第二个参数, 即V_1(objage)
IL_000a: ldloc.1 //将第二个参数压入堆栈(地址)
IL_000b: unbox [mscorlib]System.Int32 //拆箱:检查地址是否有效à返回对象内值的指针
IL_0010: ldind.i4 //取得堆栈中的指针指向的值, 然后压入堆栈
IL_0011: stloc.2 //弹出堆栈内容给第三个参数, 即V_2(iold)
IL_0012: ret //返回
} // end of method DllTestClientApp::Main