简体   繁体   中英

custom annotation / Metadata in dart lang

Can any one explain me the use of annotations in Dart?

In the documentations, I found this example:

library todo;

class todo {
  final String who;
  final String what;

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

followed by

import 'todo.dart';

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

so, what shall I write in the main() to get the doSomething() function executed?

thanks

Something like

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, []);
        };
      });
    });
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM