when I write a function with a generic like this
Task<ResultModel<bool>> CancelScheduledTask<T>(string scheduledTaskId, int year, int week) where T : TaskModel;
You have to provide the generic, because it can't be derived from the parameters
switch (TaskType)
{
case Schedule.ROUTINE:
cancelRoutineResult = await _taskRepository.CancelScheduledTask<RoutineTask>(TaskId, Year, Week);
break;
case Schedule.EVENT:
cancelRoutineResult = await _taskRepository.CancelScheduledTask<EventTask>(TaskId, Year, Week);
break;
}
Would it be possible to write this without the switch of an if else statement? something like this example which doesn't work
await _taskRepository.CancelScheduledTask<TaskType.TypeOfTask()>(TaskId, Year, Week);
public static Type TypeOfTask(this string taskType)
{
switch (taskType)
{
case Schedule.ROUTINE:
return typeof(ScheduledRoutineModel);
case Schedule.EVENT:
return typeof(ScheduledEvent);
default:
return null;
}
}