你如何获得List< Type>用反射?

编程入门 行业动态 更新时间:2024-10-25 20:20:39
本文介绍了你如何获得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&lt; Type&gt;用反射?

本文发布于:2023-11-09 06:11:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1571598.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   如何获得   List   lt   amp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!