Hi folks
I think I have asked something similar before, but here goes again...
I have to call a method that varies depending on the type of one of its
parameters.
The object that I have to pass is of a base class.
I do not want to have an "if (...) else" construct if I can avoid it.
Example methods are :
void Method1(TextBoxBase widget)
{
}
void Method2(NumericUpDown widget)
{
}
Calling method looks like this :
void CallAppropriateMethod(Control widget)
{
...
}
I thought of adding the methods to a static class :
public static class WidgetHandler
{
public static void Method(TextBoxBase widget)
{
}
public static void Method(NumericUpDown widget)
{
}
}
....and calling them, hoping that the type would be inferred and called
appropriately.
void CallAppropriateMethod(Control widget)
{
WidgetHandler.Method(widget); // code this simple would be the best !!
}
But this, of course, involves casting a base type to a specific type; not
allowed :-((
I also thought of using delegates and retrieving a particular delegate based
on a type parameter, but I couldn't get a Dictionary<Type, MyDelegate> to
work as it meant having a return from the function that would be a "base"
delegate and that I could call without knowing the type of the parameter; so
far, no success.
Generic delegates don't seem to offer the answer as you have to know the
type of the delegate you want to invoke :-((
Am I really suffering from brain-fade on this one, or is there a method I
have simply not found yet ??
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer