Get generic type of class at runtime

后端 未结 26 2595
野的像风
野的像风 2020-11-21 04:40

How can I achieve this?

public class GenericClass
{
    public Type getMyType()
    {
        //How do I return the type of T?
    }
}
26条回答
  •  醉酒成梦
    2020-11-21 05:08

    I did the same as @Moesio Above but in Kotlin it could be done this way:

    class A() {
    
        var someClassType : T
    
        init(){
        this.someClassType = (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class
        }
    
    }
    

提交回复
热议问题