调用镜像全局函数
Invoke mirrored global function
我正在我的库中搜索所有全局注释函数:
@MyAnnotation()
String func(arg1, arg2) => "test";
main() {
var routines = m.currentMirrorSystem().findLibrary(#my.lib).declarations.values
.where((m.DeclarationMirror dm) =>
dm is m.MethodMirror && dm.metadata.map((m.InstanceMirror im) => im.reflectee)
.any((refl) => refl is MyAnnotation)
);
// this works all ok
}
现在我有 List<DeclarationMirror>
个带注释的函数。但我需要稍后调用它。 ClosureMirror 有 invoke
方法,但我看不到任何方法可以为我的 MethodMirror 或例程的 Symbol 获取它。
我不能只使用 new ClosureMirror(func) 因为我有几十个带注释的函数而且我不知道每个名字。我也没有任何 ClassMirror 或 InstanceMirror。
那么问题来了,如何通过其Symbol或MethodMirror来调用全局镜像函数
m.MethodMirror f = routines[0];
(f.owner as m.LibraryMirror).invoke(f.simpleName, ['a', 'b']);
我正在我的库中搜索所有全局注释函数:
@MyAnnotation()
String func(arg1, arg2) => "test";
main() {
var routines = m.currentMirrorSystem().findLibrary(#my.lib).declarations.values
.where((m.DeclarationMirror dm) =>
dm is m.MethodMirror && dm.metadata.map((m.InstanceMirror im) => im.reflectee)
.any((refl) => refl is MyAnnotation)
);
// this works all ok
}
现在我有 List<DeclarationMirror>
个带注释的函数。但我需要稍后调用它。 ClosureMirror 有 invoke
方法,但我看不到任何方法可以为我的 MethodMirror 或例程的 Symbol 获取它。
我不能只使用 new ClosureMirror(func) 因为我有几十个带注释的函数而且我不知道每个名字。我也没有任何 ClassMirror 或 InstanceMirror。
那么问题来了,如何通过其Symbol或MethodMirror来调用全局镜像函数
m.MethodMirror f = routines[0];
(f.owner as m.LibraryMirror).invoke(f.simpleName, ['a', 'b']);