I have a generics class, Foo
. In a method of Foo
, I want to get the class instance of type T
, but I just can\'t call T.
This question is old, but now the best is use google Gson
.
An example to get custom viewModel
.
Class> clazz = new GenericClass>().getRawType();
CustomViewModel viewModel = viewModelProvider.get(clazz);
Generic type class
class GenericClass(private val rawType: Class<*>) {
constructor():this(`$Gson$Types`.getRawType(object : TypeToken() {}.getType()))
fun getRawType(): Class {
return rawType as Class
}
}