本文介绍了你如何获得List< Type>用反射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到一个方法,并检查 method.getParameterTypes()[0] ,这是一个 java.util.List 。但我想弄清楚包含类型是什么,例如,如果它是 java.util.List< String> 我想弄清楚它应该包含字符串。
I have am getting a method, and checking method.getParameterTypes()[0], which is a java.util.List. But I want to figure out what the containing type is, for instance if it's a java.util.List<String> I want to figure out that it should hold strings.
我该怎么做?
谢谢!
推荐答案Type listType = method.getGenericParameterTypes()[0]; if (listType instanceof ParameterizedType) { Type elementType = ((ParameterizedType) listType).getActualTypeArguments()[0]; }
请注意,元素类型不必是实际的 Class like String - 它可以是类型变量,通配类型等。
Note that the element type needn't be an actual Class like String -- it could be a type variable, a wildcarded type, etc.
您可以在此处中阅读有关从反映项目中抓取泛型信息的更多信息。 此处。
You can read more about scraping generics info from reflected items here and here.
更多推荐
你如何获得List< Type>用反射?
发布评论