Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting with reflection ( or othre method)

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all sorry for my bad english

i have problem that i need to cast object based on class's i have in my application , now my application ( depending on the deployment ) some time
have the classes and sometimes does not until now i did the casting with the full package name for example :

and as you can see if i miss one of the casting packages i will have compilation error .
can i do it more dynamically maybe with reflection or other way
so i could base on strings and i will have something like this :



i can get rune time exception no problem with that .
and i have the packages names as strings.
 
Henry Wong
author
Marshal
Pie
Posts: 21123
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something tells me that you have a bad design here. You are given an object, but you don't know the type, but you must cast it so that you can call the value() method?

Isn't it a better design to have an interface with the value() method? This way, you can cast it to the interface and call the method? And it doesn't matter what it is.

But to answer your question. If you want to use reflection, there is no need to cast it. With reflection, you can get a list of the methods, which you can traverse to find the value() method, and call it.

Henry
[ September 14, 2008: Message edited by: Henry Wong ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic