将 List<Class> 转换为 Class…,以获取声明的方法。

huangapple go评论61阅读模式
英文:

Convert List<Class> to Class... to get declared method

问题

我正在尝试在运行时使用 Class.getDeclaredMethod 获取加载的类的方法。Class.getDeclaredMethod 的参数是 String name, Class&lt;?&gt;... parameterTypes。我有一个作为 List 的 parameterTypes

我该如何将 parameterTypes 从我的列表传递给 Class.getDeclaredMethod

我尝试了 (Class[]) paramterTypes.toArray(),但并没有起作用。

英文:

I am trying to get a method of a class I load during runtime using Class.getDeclaredMethod. The parameters for Class.getDeclaredMethod are String name, Class&lt;?&gt;... parameterTypes. I have the parameterTypes as a List.

How do I pass the parameterTypes from my list to the Class.getDeclaredMethod?

I tried (Class[]) paramterTypes.toArray() and it didn't work.

答案1

得分: 2

你已经接近成功。只需简单地进行以下操作:

Class[] paramsAr = new Class[parameterTypes.size()];
parameterTypes.toArray(paramsAr);
英文:

You are almost there. It is simply

Class[] paramsAr = new Class[parameterTypes.size()];
parameterTypes.toArray(paramsAr);

huangapple
  • 本文由 发表于 2020年10月8日 22:43:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64265007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定