2014-11-06 36 views
1

我想没有任何成功找到了泛型类型的Iterable类(如Set<myGenericType>List<myOtherGenericType>)的泛型类型可迭代的(设置 ,列表 ...)。 查找通过内省

例如:

如果instanceMirror.getField(myField).reflecteeSet<Toto>那我也没有办法通过自省来检索类型Toto

谢谢你的帮助!

+0

似乎是http://stackoverflow.com/questions/14384865 – 2014-11-06 17:31:34

+1

没错的副本...但你的反应是比较直接的。 – matth3o 2014-11-06 18:09:09

回答

1
import 'dart:mirrors'; 

class A<T> { 
    T item; 
    List<T> items; 
} 

main() { 
    var m = reflectType(new A<int>().runtimeType); 

    var itemType = m.declarations[#item].type as TypeMirror; 
    print('type of #item: ${itemType.qualifiedName}'); // type of #item: ClassMirror on 'int'. 

    var itemsType = m.declarations[#items].type as ClassMirror; 
    print('type of #items: ${itemsType.qualifiedName}'); // type of #items: ClassMirror on 'List'. 

    print('type arguments of #items: ${itemsType.typeArguments.map((t) => t.qualifiedName)}'); // type arguments of #items: [TypeVariableMirror on 'T']. 
} 
+0

谢谢!反应非常明确 – matth3o 2014-11-06 18:07:36