获取数据所有字段的值时出错 class

Error getting the values of all fields of a data class

我正在尝试使用此扩展函数获取数据 class 的所有字段值:

fun FinancialDataResponse.asDomainModel() = FinancialData(
    heading = this::class.memberProperties.map { it.name },
    data = this::class.memberProperties.map {it.get(this).toString()}
)

但是我一直收到这个错误:

谁能告诉我在 kotlin 中获取数据 class 所有字段值的正确方法是什么?

我们将不胜感激任何形式的帮助。提前致谢!

您需要 getter.call 方法。这将起作用:

fun FinancialDataResponse.asDomainModel() = FinancialData(
heading = this::class.memberProperties.map { it.name },
data = this::class.memberProperties.map { it.getter.call(this).toString() })