Reflection: Casting NewInstance() automagically

Monkey Forums/Monkey Programming/Reflection: Casting NewInstance() automagically

Sledge(Posted 2012) [#1]
Is it possible to cast a NewInstance() object to the actual class in question using reflection? For example I was hoping the following would be possible...

Local newInstance:= currentClassInfo.Name(  currentClassInfo.NewInstance()  )


...but obviously ClassInfo.Name() doesn't work for such purposes. I could really do with creating instances of classes, the class name of which is not precisely known (ie it could be one of many classes, far too many for a Select...Case) until runtime.


slenkar(Posted 2012) [#2]
Its been a long time since I used reflection , I dont think you can,

if you explained why you need to do this other suggestions could be made.

creating a new instance may return an 'Object' but it is still the class that you want 'underneath'


Sledge(Posted 2012) [#3]
Yeah I suspect you're right that it can't be approached this way. I'm trying to get at newInstance.whateverMethod() and I'm doing it via Invoke at the moment (like this), I was just wondering if it was possible to re-factor what I have to something along the lines of what I posted. No real biggie if not.

creating a new instance may return an 'Object' but it is still the class that you want 'underneath'


It's the 'underneath' bit that's the problem because if you go...
Local newInstance:=currentClassInfo.NewInstance()

...then go...
newInstance.definiteClassMethod()

...you will get an error that the identifier of your method can't be found. And if you go...
Local newInstance:definiteClass=currentClassInfo.NewInstance()

...you'll get an conversion error from object to definiteClass. You have to cast the return from NewInstance() -- ie newInstance:=definiteClass(currentClassInfo.NewInstance()) -- if you want to then go newInstance.definiteClassMethod()

So yes, the new instance is the right class "underneath", but that's not very useful* if your code can't determine the class to cast it to via reflection. I think I'm stuck with the "longhand" (Invoke) approach.



*In my very particular situation, where I know the method name (because it is shared) but not the exact class name.

EDIT: Edited to make a bit more sense :)


slenkar(Posted 2012) [#4]
yes pretty much

if you knew an object that they inherit from you could use that

e.g. you want to create a new instance of a car, but it could be any kind of 'vehicle'
and vehicle has a method called move_forward

so you cast the newinstance to vehicle instead of car.


Sledge(Posted 2012) [#5]
Cheers Slenkar -- I just spent some time faffing around and realised the same (then rushed back here to post "Aha!") but I could so easily have missed it that I'm very thankful. I will add that the base class needs to define everything you want to access in the extended classes -- that seems to be what was tripping me up previously. Fortunately I don't need my extended classes to do anything unpredictable (I'm fudging function pointers if you hadn't guessed!) so that's me sorted :)