以下代碼是否會導致傳入GenericMethod
調用的整數被裝箱?是否使用動態關鍵字導致泛型類型導致裝箱?
void GenericMethod<T>(T value)
{
int test = (dynamic)value;
}
void Main()
{
GenericMethod(100);
}
以下代碼是否會導致傳入GenericMethod
調用的整數被裝箱?是否使用動態關鍵字導致泛型類型導致裝箱?
void GenericMethod<T>(T value)
{
int test = (dynamic)value;
}
void Main()
{
GenericMethod(100);
}
根據C#規範§4.7動態類型:
dynamic
被認爲是等同於object
除了在以下幾個方面:
上
dynamic
類型的表達式的運算可以是動態綁定(§7.2.2)。類型推理(§7.5.2)將優先於
dynamic
而不是object
,如果兩者都是候選者。
因此,鑄造dynamic
原因拳以同樣的方式作爲鑄造object
。
動態類型的變量被編譯到object類型的變量中,類型dynamic僅在編譯時存在,而不是在運行時存在。
因此,實際上您的示例會將其轉換爲引用類型的對象。拳擊這裏存在
讓我們來看看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
這盒詮釋
動態調度將比拳擊更高的數量級,所以它在這裏甚至沒有真正的相關性。 – Servy