Kotlin - 获取 KProperty1<T, out R> 的属性类型

ser*_*0ne 2 reflection kotlin

鉴于以下代码

class Foo(val bar: String)
val p = Foo::bar
Run Code Online (Sandbox Code Playgroud)

如何String从获取属性类型p

Oma*_*gra 5

如果通过获取您的意思是检查该属性是否属于类型String,则可以returnType将该属性的 与任何其他属性进行比较KType

例子

check(p.returnType == String::class.createType())
Run Code Online (Sandbox Code Playgroud)