How to get a class for executing a variable in Kotlin?

I want to get a link to the KClass executable variable. I looked at the documentation for classes and reflections, but the documentation explains how to get a static link to KClass (e.g. String::classfor String)

I need a KClass variable to execute. This is not like compiling:

fun test(x: Any) {
    val klazz = x::class
} 

How to get KClassfrom xin the above example?

+4
source share
2 answers

As said in reference , you can use .javaClass.kotlinto get the token of an KClassobject. Example:

fun printKClass(x: Any) {
    val c = x.javaClass.kotlin
    println(c)
}

KClass kotlin-reflect library , kotlin-stdlib.

+11

x::class , kotlin- .

+1

All Articles