C# 不会让我将类型T
转换为我可以证明的类型T
。我怎样才能T
转换为那种类型?
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");
}
我正在编写一个生成从 C++ 到 C# 的代码的程序。_cast
我想用这个操作代替 C++ 的operator T ()
.