【问题标题】:dart, how to get generic type of method parameter using reflection?飞镖,如何使用反射获取通用类型的方法参数?
【发布时间】:2014-05-19 17:47:13
【问题描述】:

我有测试应用:

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。这甚至可能吗?如果我检查参数类型属性为空。

【问题讨论】:

    标签: reflection dart dart-mirrors


    【解决方案1】:
    List<TypeMirror> types = mir.function.parameters.first.type.typeArguments;
    param.forEach((e) => print(e.simpleName));
    

    打印

    符号(“int”)
    符号(“布尔”)

    【讨论】:

    • 奇怪,我猜可能 dart-editors 检查工具提示中有一个错误,当它应该显示有效值时显示 null。谢谢。
    • 我也看到了。我认为调试器显示的是本地字段,而您得到的是 getter 的返回值。 getter 不一定会返回名称相似的字段的值。反射返回的类型和值有点奇怪,因为它们是一些特殊的 VM 类型,与单独在 Dart 中定义的类不同。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多