【问题标题】:Error getting the values of all fields of a data class获取数据类的所有字段的值时出错
【发布时间】:2021-05-04 14:08:29
【问题描述】:

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

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

但我不断收到此错误:

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

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

【问题讨论】:

    标签: android android-studio kotlin reflection data-class


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多