gpt4 book ai didi

reflection - Dart ,如何使用反射获取方法参数的通用类型?

转载 作者:行者123 更新时间:2023-12-03 03:13:30 29 4
gpt4 key购买 nike

我有测试应用程序:

import 'dart:mirrors';

class A {
void eventHandlerInt(List<int> x){}
void eventHandlerBool(List<bool> x){}
}

void testMirrors(aFunction){
ClosureMirror mir = reflect(aFunction);
var param = mir.function.parameters.first;
//How to get the Type T of List<T> of the first param?
}

void main() {
var a = new A();
testMirrors(a.eventHandlerInt);
testMirrors(a.eventHandlerBool);
}

我希望能够找出传递给 testMirrors的方法的第一个参数的泛型类型,因此在上面的示例中,它将是 int然后是 bool。这有可能吗?如果我检查参数,则type属性为null。

最佳答案

List<TypeMirror> types = mir.function.parameters.first.type.typeArguments;
param.forEach((e) => print(e.simpleName));

版画

Symbol("int")
Symbol("bool")

关于reflection - Dart ,如何使用反射获取方法参数的通用类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23744044/

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