samedi 12 octobre 2019

How to change a kotlin private val using reflection?

I can access a private val value using reflection as below

fun main() {
    val mainClass = MainClass()
    val f = MainClass::class.memberProperties.find { it.name == "info" }
    f?.let {
        it.isAccessible = true
        val w = it.get(mainClass) as String
        println(w)
    }
}


class MainClass {
    private val info: String = "Hello"
}

But if I want to change info, how could I do it using reflection?





Aucun commentaire:

Enregistrer un commentaire