问题描述
我必须检查 kotlin 数据类中的特定变量是否存在注释。
注解类
annotation class Test(
val identifier: String
)
数据类
data class Player(
val name: String,@property:Test("stance")
val stance: String,@property:Test("check")
val check: String
)
我必须检查此播放器对象中的特定变量是否存在 Test 注释。
fun execute(player: Player) {
//while this works,but i have to check for a specific variable
player::class.members.forEach{
val testAnnotation = it.findAnnotation<Test>()
if(testAnnotation != null) {
// DO something
}
}
I need to do something like
player.check.hasAnnotation<Test>()
}
非常感谢这里的任何帮助,TIA
解决方法
您应该使用 ::
运算符来创建成员引用
player::check.hasAnnotation<Test>()
https://kotlinlang.org/docs/reflection.html#property-references