Tag: 科特林扩展

Kotlin获得没有实例的genericstypes

嗨,想要得到types的T ,但我不能从实例得到它,我必须从类参数得到它,如何做到这一点? abstract class ViewModelFragment{ protected lateinit var mViewModel: T override fun onViewCreated(view: View, savedInstanceState: Bundle?) { mViewModel = ViewModelProviders .of(scope) .get(getGenericTClass()) // .get(mViewModel.javaClass) // not working either } inline fun getGenericTClass() = R::class.java } 现在编译器抱怨 不能使用’T’作为精炼类的types。 改用Class。 我试过使用的解决方案: https : //stackoverflow.com/a/34463352/2163045但不为我工作