Dynamic typing and overloading could help here, if you don't mind using dynamic
:
object ConvertToListIfNecessary(dynamic input)
{
return MaybeToList(input);
}
private IList<T> MaybeToList<T>(IEnumerable<T> input)
{
return input.ToList();
}
private IList<T> MaybeToList<T>(IList<T> input)
{
return input;
}
private object MaybeToList(object input)
{
// Take whatever action you want if the input *doesn't* implement
// IEnumerable<T>
}
Basically this gets the smarts behind dynamic
to do the grungy reflection work for you. It may not be quite as quick as something that's hand-rolled, but it's likely to be much simpler to get right.