簡體   English   中英

如何獲取數組元素的PropertyDescriptor?

[英]How do I get the PropertyDescriptor for the elements of an array?

我有一個如下所示的數組字段的PropertyDescriptor。

Foo[] fooArray;

如何獲取Foo的PropertyDescriptor,以便能夠獲取該類的getter和setter方法?

如果Class對象表示數組類型,則可以要求元素類型:

Class<?> clazz = Foo[].class;
assert (clazz.isArray());
assert Foo.class.equals(clazz.getComponentType());

要求一個類的PropertyDescriptor ”沒有任何意義,但也許您正在尋找BeanInfo

BeanInfo infoAboutFoo = Introspector.getBeanInfo(clazz.getComponentType());
PropertyDescriptor[] fooDescriptors = infoAboutFoo.getPropertyDescriptors();

您可以通過Class.getComponentType()訪問數組類的組件類型。

因此,為給定fooArray Foo創建BeanDescriptor

BeanDescriptor d = new BeanDescriptor(fooArray.getClass().getComponentType());

然后, BeanDescriptor允許您檢索所有Foo bean屬性的PropertyDescriptor

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM