Executing bundle of functions by their metadata tag in Dart lang

前端 未结 1 578
粉色の甜心
粉色の甜心 2020-12-22 01:24

Building on this, I want to write a code that run A:: the functions refeed by the same metadata tag.

I tunes the codes of the previous thread as below:<

相关标签:
1条回答
  • 2020-12-22 02:17
    @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, []);
      });
    }
    
    0 讨论(0)
提交回复
热议问题