2016-08-01 75 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

相關問題