【问题标题】:Getting generic information with reflection in Java [duplicate]在Java中使用反射获取通用信息[重复]
【发布时间】:2012-02-22 17:30:56
【问题描述】:

可能重复:
get type of a generic parameter in java with reflection
Java Generics Reflection: Generic field type of subclass

事情就是这样。我有这段代码,我需要以某种方式从对象中获取通用信息。

Class A {
   public static String getGenericType(Object o) {
      ...
   }

   public static void main(String[] args) {
      ArrayList&ltInteger&gt list = new ArrayList&ltInteger&gt();
      getGenericType(list); //this should return "Integer"
   }
}

我知道您可以从 Field (Class.getField) 获得泛型类型,但这是不同的。有人知道如何完成吗?提前致谢。

祝你有美好的一天

【问题讨论】:

    标签: java generics reflection


    【解决方案1】:

    是的,类型擦除意味着您在运行时不知道 内部的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-07
      • 2014-06-14
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-24
      相关资源
      最近更新 更多