如何在Dart中执行运行时类型检查? [英] How to perform runtime type checking in Dart?
问题描述
Dart规范指出:
经过修改的类型信息反映了运行时对象的类型,并且始终可以通过动态类型检查结构来查询(
类似instanceOf,casts,typecase等)。
Reified type information reflects the types of objects at runtime and may always be queried by dynamic typechecking constructs (the analogs of instanceOf, casts, typecase etc. in other languages).
听起来不错,但没有<$类似于c $ c> instanceof 的运算符。那么我们如何在Dart中执行运行时类型检查?
Sounds great, but there is no instanceof
-like operator. So how do we perform runtime type-checking in Dart? Is it possible at all?
推荐答案
instanceof-operator称为 is
在Dart中。该规范对休闲读者并不十分友好,因此,目前最好的描述似乎是 http:// www。 dartlang.org/articles/optional-types/ 。
The instanceof-operator is called is
in Dart. The spec isn't exactly friendly to a casual reader, so the best description right now seems to be http://www.dartlang.org/articles/optional-types/.
以下是示例:
class Foo { }
main() {
var foo = new Foo();
if (foo is Foo) {
print("it's a foo!");
}
}
这篇关于如何在Dart中执行运行时类型检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!