import’dart:mirrors’;const Tag =‘tag’;@标签void func()=>打印( “你好”);A级{ @标签 void func()=>打印( “你好”);}main(){ //顶级功能 打印(反映(FUNC).type.metadata)…
dart:mirrors 当然在Flutter中不起作用。
dart:mirrors
我刚刚找到了一种访问顶级函数元数据的新方法。
import 'dart:mirrors'; const Tag = 'tag'; @Tag void func() => print("hello"); main() { ClosureMirror closureMirror = reflect(func); print(closureMirror.function.metadata.first.reflectee); }
import 'dart:mirrors'; const Tag = 'tag'; @Tag void func() => print("hello"); class A { @Tag void func() => print("hello"); } main() { var mirrors = currentMirrorSystem(); // top-level func var f = mirrors.isolate.rootLibrary.declarations[#func]; print(f.metadata.first.reflectee); //method print(reflectClass(A).declarations[Symbol('func')].metadata.first.reflectee); // Closure on `func` has no metadate print(reflect(func).type.metadata); }
结果
tag tag []