【问题标题】:Invoke mirrored global function调用镜像全局函数
【发布时间】:2015-12-30 12:12:55
【问题描述】:

我正在我的库中搜索所有全局注释函数:

@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 of 例程获取此方法。 我不能只使用 new ClosureMirror(func) 因为我有几十个带注释的函数,而且我不知道每个名字。我也没有任何 ClassMirror 或 InstanceMirror。

那么问题来了,如何通过 Symbol 或 MethodMirror 调用全局镜像函数。

【问题讨论】:

    标签: reflection dart dart-mirrors


    【解决方案1】:
      m.MethodMirror f = routines[0];
        (f.owner as m.LibraryMirror).invoke(f.simpleName, ['a', 'b']);
    

    【讨论】:

    • 哦,这个可行:class AnnotatedRoutine implements Function { final m.MethodMirror 例程;升级脚本(this.routine); String call(arg1, arg2) => (routine.owner as m.LibraryMirror).invoke(routine.simpleName, [arg1, arg2]).reflectee; } 这里的降价很糟糕
    • 如果你有不同的所有者,你不需要as m.LibraryMirror,我只是添加它以获得自动完成。
    猜你喜欢
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    相关资源
    最近更新 更多