这是一个例子。
var tobeCasted = 1;
object data = null;
if (whatIsMyType == typeof(int)) {
data = (int)tobeCasted;
}
else if (whatIsMyType == typeof(float)) {
data = (float)tobeCasted;
}
然而,上面的代码是手动检测每种数据类型的。我正在寻找如下的单行通用解决方案:
data = (whatIsMyType)tobeCasted;