d

    diesieben07

    4 years ago
    I am trying to obtain the value of every field in an object, but I am apparently unable to do so. I am doing this:
    fun <T : Any> getFieldValues(obj: T): List<Any?> {
        val cls = obj::class
        return cls.memberProperties.map { it.get(obj) }
    }
    But this does not compile, because I am getting
    KClass<out T>
    for
    cls
    . Is there any way to do this in a type-safe manner?
    udalov

    udalov

    4 years ago
    No, it's a known problem: https://youtrack.jetbrains.com/issue/KT-16432 the only workaround we know is an unchecked cast 😨
    val cls = obj::class as KClass<Any>
    d

    diesieben07

    4 years ago
    Thanks for the heads up!