dart lang中的自定义注释/元数据

时间:2014-11-08 20:10:03

标签: dart dart-mirrors

任何人都可以解释我在Dart中使用注释吗?

在文件中,我找到了这个例子:

library todo;

class todo {
  final String who;
  final String what;

  const todo(this.who, this.what);
}

接着是

import 'todo.dart';

@todo('seth', 'make this do something')
void doSomething() {
 print('do something');
}

那么,我应该在main()中写什么来执行doSomething()函数?

感谢

1 个答案:

答案 0 :(得分:2)

这样的东西
import 'dart:mirrors';
import 'do_something.dart';
import 'todo.dart';


void main() {
  currentMirrorSystem().libraries.forEach((uri, lib) {
    //print('lib: ${uri}');
    lib.declarations.forEach((s, decl) {
      //print('decl: ${s}');
      decl.metadata.where((m) => m.reflectee is Todo).forEach((m) {
        var anno = m.reflectee as Todo;
        if(decl is MethodMirror) {
          print('Todo(${anno.who}, ${anno.what})');
          ((decl as MethodMirror).owner as LibraryMirror).invoke(s, []);
        };
      });
    });
  });
}