gpt4 book ai didi

generics - 通过自省(introspection)查找 Iterable (Set, List, ...) 的泛型类型

转载 作者:行者123 更新时间:2023-12-03 03:45:35 25 4
gpt4 key购买 nike

我试图找到 Iterable 的通用类型,但没有成功。类(例如 Set<myGenericType>List<myOtherGenericType> )。

例如 :

如果 instanceMirror.getField(myField).reflecteeSet<Toto>那么我没有办法检索类型 Toto通过内省(introspection)。

感谢您的帮助 !

最佳答案

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'].
}

关于generics - 通过自省(introspection)查找 Iterable (Set<E>, List<E>, ...) 的泛型类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26785669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com