Created
February 5, 2021 06:29
-
-
Save Foreman76/ecc06a568315e2db17c623739c01756f to your computer and use it in GitHub Desktop.
reflect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.int24.testreflection | |
import kotlin.reflect.full.memberProperties | |
import kotlin.reflect.full.primaryConstructor | |
data class DomenModel( | |
val id: Int = 1, | |
val name: String = "Alex", | |
val subName: String = "Pozdnyakov" | |
) | |
data class NetModel( | |
val net: Boolean, | |
val id: Int, | |
val name: String, | |
val subName: String | |
) | |
fun main(args: Array<String>) { | |
val domenModel = DomenModel() | |
val netModel = domenModel.toNetModel() | |
println(netModel) | |
} | |
fun DomenModel.toNetModel(): NetModel{ | |
return with(NetModel::class.primaryConstructor!!) { | |
val propertiesByName = DomenModel::class.memberProperties.associateBy { it.name } | |
callBy(args = parameters.associate { parameter -> | |
parameter to propertiesByName[parameter.name]?.get(this@toNetModel) | |
}) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment