如何检索标有元数据的功能 [英] How retrieve function that marks with metadata
本文介绍了如何检索标有元数据的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
查看以下代码段。
import "dart:mirrors";
class meta {
final String data;
const meta(this.data);
}
@meta("Tag")
doSomething() => print("You have to do something");
void main() {
doSomething();
}
如何获取函数,即带有元数据标签的市场?在我的示例中,我想找出哪种方法带有meta标签。
How can I retrieve functions, that is market with metadata tags? In my example, I want to find out, which method is marked with meta tags.
推荐答案
您可以执行以下操作:
you could do something like this:
void main() {
doSomething();
getMetaData();
}
void getMetaData() {
LibraryMirror currentLib = currentMirrorSystem().libraries.values.last;
currentLib.declarations.forEach((Symbol s, DeclarationMirror mirror) {
if(mirror.metadata.length > 0) {
print('Symbol $s has MetaData: "${mirror.metadata.first.reflectee.data}"');
}
});
}
这应该给您:
You have to do something
Symbol Symbol("doSomething") has MetaData: "Tag"
您还可以分析另一个项目中的文件,并对该文件使用dart:mirrors,而不用检查当前的库。也许 libraries.values.last
不会总是返回当前库-因此您可能需要更改它。就我而言,它行得通。
You could also analyze your files from another project and use dart:mirrors on that file instead of inspecting the current library. Maybe libraries.values.last
will not always return the current library - so you might need to change it. In my case it worked.
这篇关于如何检索标有元数据的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文