mardi 16 mars 2021

Kotlin Reflection - Returns an instance with set properties values based on it's property name

I'm trying to create a function that returns any data class object setting it's property values with its property names (if all strings) without changing it's default values I have an example on how it is:

Imagine this data class:

data class StudentProfile(

    var fullName: String = "",

    var mobilePhone: String = "",

    var birthDate: String = "",

    var email: String = ""

)

I want to keep this empty default values, but I wanted a generic function that should work for any class and returns (in this case) this:


return StudentProfile( 
                mobilePhone = "mobilePhone",
                fullName = "fullName",
                email = "email",
                birthDate = "birthDate"
            )

Is it possible?





Aucun commentaire:

Enregistrer un commentaire