如何判断一个对象是否是一个类的实例 [英] How to tell if an object is an instance of a class
问题描述
如何确定Dart语言中的对象是否属于类?
How can I determine whether an object is of a class or not in the Dart language?
我正在尝试执行以下操作:
I'm looking to do something like the following:
if (someObject.class.toString() == "Num") {
...
}
什么是返回值类型?一定是字符串吗?
And what is the returned value type? Will it have to be a String?
镜像库已经起伏不定,现在似乎正在快速变化,这是我的一件事确实发现并没有按图所示工作。
The mirror library has been up and down and seems to be subject to rapid change right now, as the one thing I did find simply did not work as shown.
推荐答案
-
通过使用
is
和is!
运算符,例如:if (someObject is T)
从文档:
是
和是!
运算符很方便检查类型。如果obj
实现obj的结果为T
为true > T 。以
为例,obj是Object
始终为true。The
is
andis!
operators are handy for checking types. The result ofobj is T
is true ifobj
implements the interface specified byT
. For example,obj is Object
is always true. -
使用 Mirrors API (请参见此示例):
Expect.equals('T', someObject.simpleName)
这篇关于如何判断一个对象是否是一个类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!