2016-08-01 25 views

回答

3

根据C#规范§4.7动态类型

dynamic被认为是等同于object除了在以下几个方面:

  • dynamic类型的表达式的运算可以是动态绑定(§7.2.2)。

  • 类型推理(§7.5.2)将优先于dynamic而不是object,如果两者都是候选者。

因此,铸造dynamic原因拳以同样的方式作为铸造object

0

动态类型的变量被编译到object类型的变量中,类型dynamic仅在编译时存在,而不是在运行时存在。

因此,实际上您的示例会将其转换为引用类型的对象。拳击这里存在

2

让我们来看看IL代码,看是否拳击是存在的:

IL_0000: nop 
IL_0001: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite, object, int32>> class Test.Program/'<>o__0`1'<!!T>::'<>p__0' 
IL_0006: brfalse.s IL_000a 
IL_0008: br.s IL_002e 
IL_000a: ldc.i4.0 
IL_000b: ldtoken [mscorlib]System.Int32 
IL_0010: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle) 
IL_0015: ldtoken Test.Program 
IL_001a: call class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle) 
IL_001f: call class [System.Core]System.Runtime.CompilerServices.CallSiteBinder [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.Binder::Convert(valuetype [Microsoft.CSharp]Microsoft.CSharp.RuntimeBinder.CSharpBinderFlags, class [mscorlib]System.Type, class [mscorlib]System.Type) 
IL_0024: call class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite, object, int32>> class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite, object, int32>>::Create(class [System.Core]System.Runtime.CompilerServices.CallSiteBinder) 
IL_0029: stsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite, object, int32>> class Test.Program/'<>o__0`1'<!!T>::'<>p__0' 
IL_002e: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite, object, int32>> class Test.Program/'<>o__0`1'<!!T>::'<>p__0' 
IL_0033: ldfld !0 class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite, object, int32>>::Target 
IL_0038: ldsfld class [System.Core]System.Runtime.CompilerServices.CallSite`1<class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite, object, int32>> class Test.Program/'<>o__0`1'<!!T>::'<>p__0' 
IL_003d: ldarg.0 
IL_003e: box !!T 
IL_0043: callvirt instance int32 class [mscorlib]System.Func`3<class [System.Core]System.Runtime.CompilerServices.CallSite, object, int32>::Invoke(!0, !1) 
IL_0048: stloc.0 
IL_0049: ret 

正如你可以从下面这行代码,请参阅:

IL_003e: box !!T 

这盒诠释

+0

我喜欢你的答案,因为它教会了我一些关于IL代码的事情,尽管PetSerAI只有你几分钟。我在哪个方面比较重要 – Slight

+0

谢谢,不用担心,我很高兴我的回答有些帮助。仅供参考,您可以使用任何反编译工具(例如ReSharper dotCover或Telerik的“JustDecompile”(JD) – Fabjan

+0

)检查IL代码。我过去实际使用过ILSpy几次,但从未意识到存在明确的“框”操作码。 – Slight

相关问题