如何通过反射找出字段是否为类型的instanceof? [英] How to find out if a field is instanceof a type via reflection?
本文介绍了如何通过反射找出字段是否为类型的instanceof?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过反射找出某个字段是否是某种 T
类型的实例.
I want to find out via reflection if a field is an instance of some type T
.
假设我有一个对象 o
.现在,我想知道它是否具有 T
实例的任何字段.我可以通过以下方式获取所有字段:
Lets say I have an object o
. Now I want to know if it has any fields which are instance of T
. I can get all fields with:
o.getClass().getFields();
我可以通过以下方式获取字段类型:
I can get the type of the field with:
field.getType();
但是现在我想知道此类型或任何超类型是否等于 T
.我是否必须递归调用 getSuperclass()
以确保检查所有超类型?
But now I want to know if this type or any supertype equals T
. Do I have to call getSuperclass()
recursively to be sure to check all supertypes?
推荐答案
您必须使用 查看全文