C# won’t let me cast a type T to a type I can prove T to be. How can I cast T to that type?
public static T _cast<T> (object o) {
if (typeof(T) == typeof(string)) {
return (T) o.ToString(); // Compiler YELLS!
}
throw new InvalidCastException("missing compiler generated case");
}
I’m writing a program that generates code from C++ to C#. This _cast operation I want to use in place of C++’s operator T ().
Normally I don’t answer my own questions, but I just came up with the solution: downcast to
objectbefore casting toT