jeudi 14 octobre 2021

Accessing an objects value by fieldName in Kotlin

I know I could do something like this:

data class Foo(val bar: String)

val foo = Foo("bar")

foo::class.java.getDeclaredField("bar").let {  it.trySetAccessible(); it.get(foo) }

For this tho I need to use class.java.getDeclaredField("bar") and other Java things like trySetAccessible() and get.

Is it possible to do this in a more Kotlin way?





Aucun commentaire:

Enregistrer un commentaire