Getting E type in List<E>

Hi, I want to get type of List classifier List<CustomClass> for example using reflection. How can I achieve this? Is it right using reflection?

I try:

data class Place (
    val id: Int,
    val destinations: List<String>
)

fun main () {

    val aPlace = Place::class
    aPlace.memberProperties.forEach { property ->

        val propType = property.returnType
        val propName = property.name
        val propClassName = (propType.classifier as KClass<*>).simpleName

        print("""
            propertyType = $propType
            name = $propName
            typeClassName = $propClassName
        """)
    }
}

The output that i get is:

propertyType = kotlin.collections.List<kotlin.String>
name = destinations
typeClassName = List
        
propertyType = kotlin.Int
name = id
typeClassName = Int

How to get kotlin.String in kotlin.collections.List<kotlin.String> ?

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/-k-type/arguments.html

1 Like

Holymolly! Thank you. That was quick!