gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:13:52 24 4
gpt4 key购买 nike

有人可以向我解释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()函数?

谢谢

最佳答案

就像是

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 - dart lang中的自定义注释/元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26821567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com