在运行时获取泛型类我怎样才能做到这一点?public class GenericClass<T>{
public Type getMyType()
{
//How do I return the type of T?
}}到目前为止我尝试的所有东西总是返回类型Object而不是使用的特定类型。
4 回答
开心每一天1111
TA贡献1836条经验 获得超13个赞
正如其他人所提到的那样,只有在某些情况下通过反思才有可能。
如果您确实需要该类型,这是通常的(类型安全的)解决方法模式:
public class GenericClass<T> {
private final Class<T> type;
public GenericClass(Class<T> type) {
this.type = type;
}
public Class<T> getMyType() {
return this.type;
}}添加回答
举报
0/150
提交
取消
