首先,我同意如果镜像系统允许从 Foo 和 Bar 的类型镜像以及 Map 的类镜像创建 Map 的类型镜像,那将是最好的。目前情况并非如此。
没有它,我认为您无法解决所写的问题。无法使用在编译时不被称为类型的类型参数来创建参数化类型。
为了完整起见,如果您在编译时知道类型,我将包括一种反映参数化类型的方法。如果类型参数可以表示为 Type 对象或 TypeMirror 对象以外的其他东西,您可以构建自己的表示,以允许这样的操作。
如果reflectType(Map<Foo,Baz>)
因为Map<Foo,Baz>
不是有效的类型文字而不能使用,则有一个小解决方法可以获取任何类型的类型:拥有一个具有类型参数的类以及获取该参数的类型值的方法。
import "dart:mirrors";
class Typer<T> { Type get type => T; }
main() {
var mapStringInt = reflectType(new Typer<Map<String,int>>().type);
print(mapStringInt); // ClassMirror on 'Map'
print(mapStringInt.typeArguments); // [ClassMirror on 'String', ClassMirror on 'int']
// That is: it's a TypeMirror on Map<String,int>.
}