Kotlin:javaSetter 方法未编译

Kotlin: javaSetter method is not compiling

我使用

检索了 class 的 属性
 val prop = businessObject::class.memberProperties.first()

我能做到:

prop.javaGetter

但是这个方法编译不通过:

prop.javaSetter

即使该方法存在且未弃用

它没有编译因为 memberProperties is a Collection<KProperty1>, and KProperty1 doesn't have any javaSetter property. But you can test if the property is in fact a KMutableProperty1, and if it is, after a cast or a smart cast, use its javaSetter 属性.

如您所见,文档很有帮助。使用它。