如何判断一个对象是否是一个类的实例 [英] How to tell if an object is an instance of a class

查看:216
本文介绍了如何判断一个对象是否是一个类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定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 and is! operators are handy for checking types. The result of obj is T is true if obj implements the interface specified by T. For example, obj is Object is always true.


  • 使用 Mirrors API (请参见此示例):

    Expect.equals('T', someObject.simpleName)
    


  • 这篇关于如何判断一个对象是否是一个类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆