You can use a helper class and dynamic
type to skip compile-time checks:
sealed class CountDuplicatesFastCaller
{
public int Call(IList list) where T : IComparable
{
return CountDuplicatesFast(list);
}
}
public static int CountDuplicates(IList list)
{
if (typeof (IComparable).IsAssignableFrom(typeof (T)))
{
return ((dynamic) new CountDuplicatesFastCaller()).Call(list);
}
else
{
/* use the slow algorithm */
}
}
This should be faster than pure reflection because of DLR caching mechanisms.