I'm trying to make a function that can take in a parameter of class Parent that is actually of either type Child1, Child2, or Child3. I want to do something different based on which child type it is and I can't think of a way to do it other than what I am showing below. This feels wrong to me so any suggestions for a better way to do this would be much appreciated.
public static bool DoStuff(Parent parent)
{
try
{
Child1 child = parent as Child1;
DoStuffChild1(child);
}
catch (Exception)
{
try
{
Child2 child = parent as Child2;
DoStuffChild2(child);
}
catch(Exception)
{
try
{
Child3 child = parent as Child3;
DoStuffChild3(child);
}
catch (Exception)
{
HandleError();
}
}
}
}