首页 > 其他 > 详细

通过反射获取列表属性里保存的对象类型

时间:2015-04-08 12:27:46      阅读:194      评论:0      收藏:0      [点我收藏+]

假设有个对象里有一个list<Object> listProperty 属性,要获取Object的类型:

Class<?> typeClass = field.getType();
if (List.class.isAssignableFrom(typeClass)) {
      Type fieldType = field.getGenericType();
       if (fieldType instanceof ParameterizedType) {
               ParameterizedType paramType = (ParameterizedType) fieldType;
               Type[] genericTypes = paramType.getActualTypeArguments();
               if (genericTypes != null && genericTypes.length > 0) {
                        if (genericTypes[0] instanceof Class<?>) {
                              Class<?> subType = (Class<?>) genericTypes[0];
                        }
               }
       }
}

其中subType就是Object的对象类型。

通过反射获取列表属性里保存的对象类型

原文:http://www.cnblogs.com/BensonHe/p/4401783.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!