I am looking for an elegant way to call a function based on the type of parameter that is passed as parameter.
In other words, I want the EntryPoint method (below) to dynamically call the appropriate myFunc method, based on the type of the template parameter.
public void EntryPoint(object template)
{
missingMethod(template);//This is the code in question that should call myFunc
}
private void myFunc(TemplateA template)
{
doSomething(template);
}
private void myFunc(TemplateB template)
{
doSomethingElse(template);
}
private void myFunc(object template)
{
throw new NotImplementedException(template.GetType());
}
Three options:
Personally I’d try to think of an alternative design which didn’t require this in the first place, but obviously that’s not always realistic.