samedi 8 février 2020

Convert a data class instance to a map via reflection and maintain order in Kotlin

I trying to use data classes to generate a CSV file. Each row is to be represented by the same data class. To go through the data class fields, I am using:

        val props = dataClass::class.memberProperties.associateBy { it.name }
        return props.keys.associateWith { props[it]?.get(this) }

The problem is that it doesn't maintain order and I cannot generate the CSV file correctly. Is there any way how to overcome this issue? My only idea is order of constructor arguments, but not sure whether it is a correct way.





Aucun commentaire:

Enregistrer un commentaire