Joshua Foulk

Joshua Foulk

  • NA
  • 18
  • 0

Call Function without knowing inherited class type

Jan 26 2009 9:12 PM

Let's say I have two classes called "Thing" and "Thang" that both inherit from "Widget".  I know that the base class of Thing and Thang is a Widget, but when I'm using a reference to one from another class, I don't know which one I'm currently using.  Let's also say that Thing and Thang both have a function that has the same name, but have to be defined within, and executed from, the derived class (Thing or Thang) for one reason or another.

Here is what I know:

1) The name of the function I need to call.

2) The parameters I need to pass to the function.

Here's what I don't know

1) Whether I'm currently calling this function on Thing or Thang (so I can't just cast to the right type and call the function)

What I would like to know is this...  Is there any way to call the function on Thing or Thang (regardless of which one it is because I have a reference to the exact object, I just don't know which type it is) without knowing the type?

I know that I could just do a GetType() on my reference and use some not-so-clever conditionals, but this is very messy and will require a lot of maintenance as I continue to subclass Widget.  Any other options?

Thanks,

Joshua Foulk


Answers (2)