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

2024-02-01

谁能给我解释一下 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() 函数呢?

thanks


就像是

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, []);
        };
      });
    });
  });
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

dart lang 中的自定义注释/元数据 的相关文章

随机推荐