且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Dart通过函数名称调用成员函数

更新时间:2023-10-27 09:26:28

我不希望读者思考发问者的想法Dart中无法满足需要,因此我添加了一个答案。

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

如果名称可以作为字符串使用,则需要使用Mirrors来调用方法。例如:

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