2014-11-09 5 views
1

에있는 메타 데이터 태그로 함수 번들을 실행 this에 빌드를 작성하면 동일한 메타 데이터 태그로 기능을 다시 설정하는 코드 A::을 실행하는 코드를 작성하고 싶습니다. 내가 얻을 출력, 위의 사용Dart lang

library main; 
import 'getFunctionMirrorsByTag.dart'; 
import 'extra.dart'; 
@Tag(#foo) 
    printa()=>print('a'); 

@Tag(#foo) 
    printb()=>print('b'); 


void main() { 
    print(getClassMirrorsByTag(#foo)); 
} 

:

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 :

나는 다음과 같이 이전 스레드의 코드를 조정한다 is :

[MethodMirror on 'printa', MethodMirror on 'printb'] 

답변

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, []); 
    }); 
} 
+0

큰 감사 @JAre –