简体   繁体   English

Kotlin 将 Annotation 作为参数传递给 Annotation

[英]Kotlin Pass Annotation as parameter to Annotation

In Kotlin, I am trying to create an Annotation which can accept an array ( vararg ) of other annotations.在 Kotlin 中,我试图创建一个可以接受其他注释的数组( vararg )的注释。

I'm trying to do something like this:我正在尝试做这样的事情:

annotation class MyAnnotation(
  vararg val annotation: Annotation
)

The problem is it tells me Annotation is not a valid parameter for annotation.问题是它告诉我Annotation不是Annotation的有效参数。 If I try a specific annotation class, it works.如果我尝试一个特定的注释类,它会起作用。

I'm trying to create an annotation which can take other annotations (for validations) and process them in order and do one unified effect.我正在尝试创建一个注释,该注释可以采用其他注释(用于验证)并按顺序处理它们并执行统一的效果。

Is there some special Kotlin syntax I can use to get it to let me provide an array of various annotation?是否有一些特殊的 Kotlin 语法可以用来让我提供各种注释的数组?

I believe anything that is nullable (interfaces, Java types...) cannot be used as parameter to a Kotlin annotation.我相信任何可为空的(接口、Java 类型...)都不能用作 Kotlin 注释的参数。 Maybe you could use也许你可以用

annotation class MyAnnotation(vararg val annotations: KClass<in Annotation>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM