自定义注释/ dart lang中的元数据 [英] custom annotation / Metadata in dart lang

查看:1022
本文介绍了自定义注释/ dart lang中的元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);
}

后跟

import 'todo.dart';

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

所以,我应该在main()中写什么来获取doSomething )function executed?

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

感谢

推荐答案

>



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

这篇关于自定义注释/ dart lang中的元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆