Assume this method:
public T GetParameterValue<T>(string ParamName) {
if(typeof(T) == typeof(Boolean?) && Request.QueryString.AllKeys.Contains(ParamName)) {
Boolean? istrue = null;
if(Request.QueryString.GetValues(ParamName).FirstOrDefault() == "1")
istrue = true;
else if(Request.QueryString.GetValues(ParamName).FirstOrDefault() == "0")
istrue = false;
return (T)Convert.ChangeType(istrue, typeof(T));
}
//Other types implementation
}
所以这个方法总是在返回行引发异常:
Invalid cast from 'System.Boolean' to 'System.Nullable`1[[System.Boolean,
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]'.
我不明白问题出在哪里。我没有使用 Boolean
而是使用了 Boolean?
。
这是我的调用行:
Product.IsAllow= GetParameterValue<Boolean?>("IsAllow");
那么,您对此有什么想法?
isTrue=Request.QueryString.GetValues(ParamName).FirstOrDefault() == "1"
。 - JohnFx!=“1”
并不一定意味着==“0”
。 - Dan Puzey