我具有以下功能
void (Type type, dynamic instance) {
// Unable to do this
if (instance is type) {
}
}
我想检查传递的实例是否与传递的type
类型相同。 type
可以是基类,而instance
可以是基类的实现,因此仅(instance.runtimeType == type)
是不够的。
答案 0 :(得分:1)
使用运行时Type
对象不能这样做。
要实现类似的目的,您必须改为使用通用函数:
void foo<T>(dynamic instance) {
if (instance is T) {
}
}
答案 1 :(得分:0)
只有一种有限的方法可以像使用type.toString()
if('$type' == 'SomeClassName')