Wondering if using “dynamic” is the only way to use an overload from within a generic type. The (int)(object) prefix is a little cumbersome.
public class TOfTestCase<T> {
public void otherMethod(int arg1) {
}
public void method(T arg1) {
if (typeof(T) == typeof(int)) {
otherMethod((int)(object)arg1);
}
}
}
Yes, I’m afraid it is. You need to effectively fool the C# compiler into not trying to look for a more meaningful conversion.
Personally I usually find that if a method can only handle certain types, it’s better to have overloads rather than to make it generic in the first place, but it does depend on the situation.
If you’re really trying to perform overloading (there’s no overloading shown in your sample code, as there’s only a single method with each name) and if you’re using C# 4, you could use dynamic typing:
This will fail at execution time if there isn’t a suitable overload, of course.