gpt4 book ai didi

dart - 如何获取声明的注释

转载 作者:行者123 更新时间:2023-12-03 03:09:05 25 4
gpt4 key购买 nike

根据规范,元数据可以出现在变量声明之前。

但是,它没有说明是否可以检索。

const annotation = null;

main() {

@annotation
var a = "";
print(reflect(a) is DeclarationMirror);
}

输出 false;

这样的注释用法是否可以检索?

最佳答案

对不起,anwser,但实际上,您无法做自己想做的事情。
出于简单的原因:您的代码在语法上是正确的,但是没有创建注释的实例。
例如:

class Testing {
Testing(String toPrint) {
print(toPrint);
}
}

class annotation {
final Testing test;
const annotation(this.test);
}

void main() {
@annotation(new Testing("Annotation instanciation")) var a = "hello";

print(a);
var annot = new annotation(new Testing("Local instanciation"));
print(a);
}
此代码结果:

$ hello

$ Local instanciation

$ hello


因此,注释构造函数从未被调用过。
可能将来会添加此功能
信息:
实际上,在这种情况下它不起作用,因为它是局部变量声明。对于功能,类或其他功能,它将起作用。

关于dart - 如何获取声明的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23904314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com