没有拳击。
你可以把你的代码放进sharplab
here
. 不
box
可以
在某些情况下是盒子,但在这里不是。
如果将结构强制转换为其接口类型,则将显示以下框:
MyInterface x = new MyStruct();
但是,像这样调用泛型方法(无论泛型类型参数是否被约束到接口类型)并不会阻塞。当你打电话的时候
MyExtensions.DoSomethingElse<MyStruct>
,方法的新实现由JIT发出,它专门使用
MyStruct
MyExtensions.DoSomethingElse
做一个
constrained virtual call
调用
DoSomething
方法。这主要是在编译器不确定目标在运行时是值还是引用类型时使用的,它基本上是说“JIT,你来解决这个问题”。明确地:
-
thisType
是值类型,并且
这种类型
method
然后
ptr
作为指向
call
方法
方法
通过
.
-
是值类型,并且
这种类型
不执行
然后
callvirt
方法
说明。
t.GetHashCode()
或
t.GetType()
,则JIT将向box发出指令
t