How to get generic parameter class in Kotlin

后端 未结 5 801
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 09:30

Firebase\'s snapshot.getValue() expects to be called as follows:

snapshot?.getValue(Person::class.java)

However I would like t

5条回答
  •  难免孤独
    2020-12-05 09:44

    Using typeOf is another option. (Added in Kotlin 1.3)

    Example:

    @OptIn(ExperimentalStdlibApi::class)
    inline fun  someMethod(data: T) {
    
        val typeClassifier = typeOf().classifier
    
        if (typeClassifier == List::class) {
            //Do something
        }
    
    }
    

提交回复
热议问题