How to convert a Kotlin data class object to map?

前端 未结 6 1548
一个人的身影
一个人的身影 2020-12-09 02:39

Is there any easy way or any standard library method to convert a Kotlin data class object to a map/dictionary of it\'s properties by property names? Can reflection be avoid

6条回答
  •  星月不相逢
    2020-12-09 03:21

    The closest you can get is with delegated properties stored in a map.

    Example (from link):

    class User(val map: Map) {
        val name: String by map
        val age: Int     by map
    }
    

    Using this with data classes may not work very well, however.

提交回复
热议问题