【发布时间】:2014-02-24 14:02:26
【问题描述】:
我是要动态返回一个类型的默认值,但是我不能将默认关键字传递给Type类型的变量。
为什么不呢?
例如:
private object GetSpecialDefaultValue(Type theType)
{
if (theType == typeof (string))
{
return String.Empty;
}
if (theType == typeof (int))
{
return 1;
}
return default(theType);
}
给我编译时错误:
找不到类型或命名空间名称“theType”(您是 缺少 using 指令或程序集引用?)
【问题讨论】:
-
因为默认值是在编译时解析的。因为你给它的类型在编译时是未知的,所以它不起作用。
标签: c#