如何检索声明上的注释 [英] How to retrieve annotations on declarations

查看:187
本文介绍了如何检索声明上的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

According to the spec, metadata can appear before a variable declaration.

但是,它不会说如果这是可能检索。

However, it does not say anything about if this is possible to retrieve.

const annotation = null;

main() {

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

输出 false

是否可以检索这种注释的使用?

Is the retrieval of such usage of annotations possible?

推荐答案

不,这是不可能的。

你是正确的,你需要使用镜像来检索元数据。规范说:

You are correct that you need to use mirrors to retrieve metadata. The spec says:

"Metadata can be retrieved at runtime via a reflective call, provided the annotated
 program construct p is accessible via reflection."

但是,没有用于局部变量(在函数体内声明的变量)的镜像,不能通过反射访问。

However, there are no mirrors for local variables (variables declared inside function bodies), so it is exactly not accessible via reflection.

您只能找到顶层的声明,类的成员或函数的参数的镜像(以及技术上的局部函数声明,但是这不工作很好)。

You can only find mirrors for declarations that are either top-level, members of a class, or parameters of a function (and, technically, a local function declaration, but that doesn't work very well).

这篇关于如何检索声明上的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆