Reflection 调用函数时发生Kotlin反射错误

Reflection 调用函数时发生Kotlin反射错误,reflection,kotlin,Reflection,Kotlin,我试图使用kotlin原语转换方法将未知的给定对象强制转换为所需的类,如下所示: private fun callSelfParser(origObj: Any, destClazz: KClass<*>): Any { val methodName = when (destClazz) { Int::class -> "toInt" Char::class -> "toChar" L

我试图使用kotlin原语转换方法将未知的给定对象强制转换为所需的类,如下所示:

private fun callSelfParser(origObj: Any, destClazz: KClass<*>): Any {
        val methodName = when (destClazz) {
            Int::class -> "toInt"
            Char::class -> "toChar"
            Long::class -> "toLong"
            Byte::class -> "toByte"
            Float::class -> "toFloat"
            Short::class -> "toShort"
            Double::class -> "toDouble"
            Boolean::class -> "toBoolean"
            else -> ""
        }

        val parsingMethod: KFunction<*>
        try {
            parsingMethod = origObj::class.functions.toList().first { it ->
                it.name == methodName
            }
        } catch (e: NoSuchElementException) {
            throw ObjectMapperEx("Element $origObj doesn't have a method for parsing to $destClazz")
        }

        return parsingMethod.call(origObj)!!
    }

有人能说有什么不对吗?或者,如果有其他方法可以实现我的目标的话?

您使用的是哪一版本的kotlin?@jrtapsell 1.2.21 kotlin似乎无法使用扩展函数的
。调用
。因为对于
toString
来说,它工作得很好。为什么要在
destClass
中搜索函数?@jrtapsell是的,它是错误的。但还有什么选择呢?
Exception in thread "main" kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Reflection on built-in Kotlin types is not yet fully supported. No metadata found for public open val length: kotlin.Int defined in kotlin.String[DeserializedPropertyDescriptor@1cbb87f3]