本文介绍了在Dart lang中通过其元数据标记执行函数包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据,我想写一个运行 A :: 由相同元数据标签重新提交的函数的代码。



我调整以前的代码线程如下:



getFunctionMirrorsByTag.dart

  library impl; 
@MirrorsUsed(metaTargets:Tag)
import'dart:mirrors';

类标记{
final符号名称;
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); //我想用执行返回函数的语句$ b}
});
});
});
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'] 
/ pre>

解决方案
  @MirrorsUsed(metaTargets:Tag) 
import'dart:mirrors';

类标记{
final符号名称;
const Tag(this.name);
}
List getMirrorsByTag(Symbol name){
List res = new List();
MirrorSystem ms = currentMirrorSystem();
ms.libraries.forEach((u,lm){
lm.declaration.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,[]);
});
}


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:

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));
 }

Using the above, the output I get is:

[MethodMirror on 'printa', MethodMirror on 'printb']
解决方案
@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, []);
  });
}

这篇关于在Dart lang中通过其元数据标记执行函数包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 03:29