在this上构建,我想编写一个运行A::
由相同元数据标记重新输入的函数的代码。
我调整上一个帖子的代码如下:
getFunctionMirrorsByTag.dart
library impl;
@MirrorsUsed(metaTargets: Tag)
import 'dart:mirrors';
class Tag {
final Symbol name;
const Tag(this.name);
}
List<ClassMirror> getClassMirrorsByTag(Symbol name) {
List res = new List();
MirrorSystem ms = currentMirrorSystem();
ms.libraries.forEach((u, lm) {
lm.declarations.forEach((s, dm) {
dm.metadata.forEach((im) {
if ((im.reflectee is Tag) && im.reflectee.name == name) {
res.add(dm); // I want to replace this by a statement executing the returned function
}
});
});
});
return res;
}
main.dart:
library main;
import 'getFunctionMirrorsByTag.dart';
import 'extra.dart';
@Tag(#foo)
printa()=>print('a');
@Tag(#foo)
printb()=>print('b');
void main() {
print(getClassMirrorsByTag(#foo));
}
使用上面的内容,我得到的输出是:
[MethodMirror on 'printa', MethodMirror on 'printb']
答案 0 :(得分:2)
@MirrorsUsed(metaTargets: Tag)
import 'dart:mirrors';
class Tag {
final Symbol name;
const Tag(this.name);
}
List getMirrorsByTag(Symbol name) {
List res = new List();
MirrorSystem ms = currentMirrorSystem();
ms.libraries.forEach((u, lm) {
lm.declarations.forEach((s, dm) {
dm.metadata.forEach((im) {
if ((im.reflectee is Tag) && im.reflectee.name == name) {
res.add(dm);
}
});
});
});
return res;
}
@Tag(#foo)
printa() => print('a');
@Tag(#foo)
printb() => print('b');
void main() {
getMirrorsByTag(#foo).forEach((MethodMirror me) {
LibraryMirror owner = me.owner;
owner.invoke(me.simpleName, []);
});
}