Dart calling a member function by function name [duplicate]

▼魔方 西西 提交于 2019-12-11 11:22:26

问题


I am wondering if there is anyway to call a function by its name in dart as in javascript.

I would like to do something as such:

foo["bar"]();

回答1:


I don't want readers to think what the questioner wants isn't possible in Dart, so I'm adding an answer.

You need to use Mirrors to call a method if you have its name available as a string. Here is an example:

import 'dart:mirrors';

class Foo {
  bar() => "bar";
}

void main() {
  var foo = new Foo();

  var mirror = reflect(foo);
  print(mirror.invoke(#bar, []).reflectee); // Prints 'bar'.
}


来源:https://stackoverflow.com/questions/19718821/dart-calling-a-member-function-by-function-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!