次のようなメソッドがあります。このクラスは、現在 2 つの異なるクラスを渡すことができるオブジェクトのリストを保持しています。
public class DataTableEntity {
List<Objects> objectContainer;
public void setLinks(Class cls){
for(Object obj : objectContainer){
//the cls variable will hold what type of object it is
//so I need to somehow say if its a 'dog' then the obj I am going through is a dog
// and the method is dog.getName else it might be cat and cat.getCatName etc.
}
}
この関数は、リストが具体的に何であるかを通知され、クライアントに送信される前に、そのリスト内のオブジェクトのプロパティを正しくフォーマットする必要があります。オブジェクトが異なれば、これらのプロパティのセッター名とゲッター名も異なります。したがって、どうにかしてジェネリック クラス オブジェクトを取得し、それが実際にどのような種類のクラスであるかを確認する必要があります。これにより、「.getAbc/.setAbc」または「.getXyz/.setXyz」のどのメソッドを使用するかを知ることができます。
これは可能ですか?