Search code examples
kotlinreflectiontypeof

How to check if class property is type of List in Kotlin


How can I check if an attribute of a class is type of List? The following code snippet is what I want but it doesn't work.

class MyClass(val attr:List<String>)
fun main(args: Array<String>) {
    var prop = MyClass::attr
    if(prop.returnType.classifier is List<*>)
        println("Property is type of List")
}

Solution

  • use

    prop.returnType.classifier == List::class
    

    instead of

    prop.returnType.classifier is List<*>