日期:2014-05-18  浏览次数:21198 次

一段装箱的MSIL代码,大家帮忙解释下什么意思?
原代码:
using System;
namespace StructApp

public class BoxAndUnBox
{
public BoxAndUnBox()

}
static void Main(string[] args)
{
double dubBox = 77.77; /// 定义一个值形变量 
object objBox = dubBox; /// 将变量的值装箱到 一个引用型对象中 
Console.WriteLine("The Value is '{0}' and The Boxed is {1}",dubBox,objBox.ToString());
}
}
}

MSIL代码:
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 代码大小 40 (0x28)
.maxstack 3
.locals init ([0] float64 dubBox,
[1] object objBox)
IL_0000: ldc.r8 77.769999999999996
IL_0009: stloc.0
IL_000a: ldloc.0
IL_000b: box [mscorlib]System.Double
IL_0010: stloc.1
IL_0011: ldstr "The Value is '{0}' and The Boxed is {1}"
IL_0016: ldloc.0
IL_0017: box [mscorlib]System.Double
IL_001c: ldloc.1
IL_001d: callvirt instance string [mscorlib]System.Object::ToString()
IL_0022: call void [mscorlib]System.Console::WriteLine(string,
object,
object)
IL_0027: ret
} // end of method BoxAndUnBox::Main
看了下MSIL手册,还是不太懂,大家帮忙看看,能逐句解释下最好了,谢谢了,分不多请见谅!

------解决方案--------------------
我来试一下。
.method private hidebysig static void Main(string[] args) cil managed 

.entrypoint 
// 代码大小 40 (0x28) 
.maxstack 3 
.locals init ([0] float64 dubBox, 
[1] object objBox) //local变量声明
IL_0000: ldc.r8 77.769999999999996 //将77.769999999999996加载到堆栈上
IL_0009: stloc.0 //将堆栈上的值77.769999999999996赋给local第一个变量dubBox
IL_000a: ldloc.0 //将local的第一个变量dubBox加载到堆栈上
IL_000b: box [mscorlib]System.Double //这里开始装箱操作,因为下面要将该值赋给一个object变量。
IL_0010: stloc.1 //将装箱后的值赋给local的第二个变量objBox。
IL_0011: ldstr "The Value is '{0}' and The Boxed is {1}" //将字符串加载到堆栈上。
IL_0016: ldloc.0 //将第一个变量dubBox加载到堆栈上。
IL_0017: box [mscorlib]System.Double //这里开始装箱操作,因为Console.WriteLine方法的参数是object。
IL_001c: ldloc.1 //将第二个变量objBox加载到堆栈上。
IL_001d: callvirt instance string [mscorlib]System.Object::ToString() //调用objBox的ToString方法,将返回值加载到堆栈上。
IL_0022: call void [mscorlib]System.Console::WriteLine(string, 
object, 
object) //调用Console.WriteLine。
IL_0027: ret //返回
} // end of method BoxAndUnBox::Main