我正在寻找一种优雅的方法来根据作为参数传递的参数类型调用函数.
换句话说,我希望EntryPoint方法(下面)根据template参数的类型动态调用适当的myFunc方法.
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());
}
Run Code Online (Sandbox Code Playgroud)