我想去获取Type
,T
但我无法从实例中获取它。我必须从类参数中获取它,该怎么做?
abstract class ViewModelFragment<T : ViewModel>{
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<reified R> getGenericTClass() = R::class.java
}
现在编译器抱怨
不能使用“T”作为精炼类类型。改用类。
我尝试使用此答案中的解决方案,但对我不起作用