I have the following code:
switch (objectType)
{
case ObjectType.UserReview:
return MyMethod<UserReview>();
case ObjectType.ProfessionalReview:
return MyMethod<ProfessionalReview>();
case ObjectType.Question:
return MyMethod<Question>();
case ObjectType.News:
return MyMethod<News>();
default:
throw new ArgumentOutOfRangeException("objectType");
}
Now I need the same code but for calling another method. Are there any options to implement it without duplication and Reflection?
Sounds like an ideal opportunity to replace conditional with polymorphism. Can you promote
ObjectType.UserReviewto be a class (it looks like anenumat the moment) and put the switch block as a polymorphic method?