欲验证的结论: 接口引用指向装箱后的值类型时无需拆箱 。
代码如下:
interface SomeInterface
{
void MethodInInterface();
}
struct OneValueType : SomeInterface
{
public void MethodInInterface()
{
Console .WriteLine( "get called" );
}
}
class TestUnBox
{
public static void Main()
{
}
private static void CastStruct()
{
OneValueType ovt = new OneValueType ();
object o = ovt; //Box
(( OneValueType )o).MethodInInterface();
}
private static void CastInterface()
{
OneValueType ovt = new OneValueType ();
object o = ovt; //Box
(( SomeInterface )o).MethodInInterface();
}
}
其中 OneValueType 是值类型,它实现了 SomeInterface 。
两个方法CastStruct 和 CastInterface 分别把装箱之后的值类型转型为 OneValueType 和 SomeInterface 。预计的结果是第二个方法无需拆箱,查看 IL 来验证:
这是CastStruct ():
这是CastInterface ():
可见,第二个方法中没有unbox 指令,结论得证。
2009年 7 月 29 日