以下代码是否会导致传入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