看看这个方法:
Dictionary<string,object> ViewModelParams = new Dictionary<string,object>();
AddParam(string paramKey,value)
{
viewModelParams.Add(paramKey,value);
}
T GetParam<T>(string paramKey)
{
if(viewModelParams[paramKey] is T)
return (T)viewModelParams[paramKey];
else
throw exception...
}
对于Nullable类型的表达式if(viewModelParams[paramKey] is T)
,如果字典中的值为 null,则无法正常工作,以澄清:
int? item=null;
AddParam("key",item);
GetParam<int?>("key")// throw exception because of
// (viewModelParams[paramKey] is T) is false
我知道可空类型( Boxing Nullable Types )的装箱和拆箱概念,但我不知道if(viewModelParams[paramKey] is T)
在这种情况下用哪个表达式替换?