且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

取消装箱动态类型

更新时间:2023-12-04 23:42:40

是开箱 - 但它是隐式做。有一个从任何动态表达式到任何类型的隐式转换。

It is unboxing - but it's doing it implicitly. There's an implicit conversion from any dynamic expression to any type. The exact conversion performed will depend on the execution-time type of the value.

从C#5规范的第6.1.8节开始:

From section 6.1.8 of the C# 5 specification:


隐式动态转换从动态的表达式存在到任何类型 T 。转换是动态绑定的(§7.2.2),这意味着将在运行时从表达式的运行时类型到 T 查找隐式转换。如果没有找到转换,则会抛出运行时异常。

An implicit dynamic conversion exists from an expression of type dynamic to any type T. The conversion is dynamically bound (§7.2.2), which means that an implicit conversion will be sought at run-time from the run-time type of the expression to T. If no conversion is found, a run-time exception is thrown.

(这里有一点细微差别,类型动态表达式,而不是从动态类型转换,转换循环,这将导致spec中其他地方的问题。)

(There's a slight nuance here in that it's a conversion from any expression of type dynamic rather than a conversion from the dynamic type itself. That avoids some conversion loops which would cause issues elsewhere in the spec.)