【问题标题】:How to check if class property is type of List in Kotlin如何检查类属性是否是 Kotlin 中的 List 类型
【发布时间】:2019-12-27 19:44:40
【问题描述】:

如何检查类的属性是否为 List 类型?下面的代码 sn -p 是我想要的,但它不起作用。

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")
}

【问题讨论】:

    标签: kotlin reflection typeof


    【解决方案1】:

    使用

    prop.returnType.classifier == List::class
    

    而不是

    prop.returnType.classifier is List<*>
    

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 2015-02-19
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 2014-11-05
      • 2014-03-09
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多