Saturday, April 12, 2014

Given a ArraryList object, how to use reflection to get the type of its members


void Parse (ArrayList list)
{
/ / How to get the list according to the type of the incoming elements, then instantiate element (assuming elements have a default constructor)
...
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
obj.getClass (). getSimpleName ()
Or use instanceof to determine the object
Reply:
cited a floor guoyoulei520 reply:.
obj.getClass () getSimpleName ()
Or use instanceof to determine the object


No way, I do not know the function of the type of the list, so you can not use instanceof,
List object obj.getClass (). GetSimpleName () returns a ArrayList, not an element of the class name information
Reply:
reference to the second floor book_jb9802 reply:
Quote: references to a floor guoyoulei520 reply:

obj.getClass (). getSimpleName ()
Or use instanceof to determine the object


No way, I do not know the function of the type of the list, so you can not use instanceof,
List object obj.getClass (). GetSimpleName () returns a ArrayList, not an element of the class name information

list.get (0) ah, ah like taking
Reply:
reference to the third floor guoyoulei520 reply:
Quote: references to the second floor book_jb9802 reply:

Quote: references to a floor guoyoulei520 reply:

obj.getClass (). getSimpleName ()
Or use instanceof to determine the object


No way, I do not know the function of the type of the list, so you can not use instanceof,
List object obj.getClass (). GetSimpleName () returns a ArrayList, not an element of the class name information

list.get (0) ah, like this take ah


No way, at the beginning of the list is empty, fill my function is to complete the list of elements.
Reply:
Why should this design, ah, to find an architect to help you plan the next
Reply:
Write a json object automatically decomposed into a class module
Reply:
Essentially get generic type
See http://lorry1113.iteye.com/blog/973903
Reply:
reference to the 7th floor CuGBabyBeaR reply:
essentially get generic type
See http://lorry1113.iteye.com/blog/973903


Very classic, I quest is this code, thank you

No comments:

Post a Comment