简体   繁体   English

获取参数化类型的java反射

[英]get paramterized type java reflection

Is there anyway to get the inner type parameter of a field using reflection 无论如何有使用反射获取字段的内部类型参数

For Ex: 例如:

public final class Main
{
        class A
        {
                public int aX;
                public String aS;
        }

        class B
        {
                public Set<A> bSet;
        }
        public static void main(String[] args)
        {
                Class clazz = B.class.getField("bSet").getType();
        }
};

Using the above I can only get "Set" is there anyway I could get A as well ? 使用上面的我只能得到“ Set”,反正我也可以得到A? Thanks 谢谢

Reflection can indeed deliver full parameterized types for fields (and classes, etc). 反射确实可以为字段(和类等)传递完整的参数化类型。 It's only objects that have their types erased at runtime. 只是在运行时删除其类型的对象

Call getGenericType instead of getType and start reading javadoc. 调用getGenericType而不是getType并开始阅读javadoc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM