我有以下方法:
public static T Deserialize<T>(vRS rs) where T : class, new() {
T o = new T();
Type tp = typeof(T);
foreach (PropertyInfo pi in tp.GetProperties()) {
string name = getBlParameterName(pi);
Type pt = pi.PropertyType;
if (pt == typeof(string)) {
String s = rs.getString(name);
//try {
pi.SetValue(o, s, null);
//pi.SetValue(o, s, (object[])null);
//} catch {
//throw new Exception("" + o.GetType().FullName);
//}
} else if (pt == typeof(int)) {
int i = rs.getInt(name);
pi.SetValue(o, i, null);
} else if (pt == typeof(int?)) {
int? i = rs.getInt(name);
if (i == int.MinValue) i = null;
pi.SetValue(o, i, null);
} else if (pt == typeof(short)) {
short i = (short)rs.getInt(name);
pi.SetValue(o, i, null);
} else if (pt == typeof(short?)) {
short? i = (short)rs.getInt(name);
pi.SetValue(o, i, null);
} else if (pt == typeof(DateTime)) {
DateTime dt = rs.getDate(name);
pi.SetValue(o, dt, null);
} else if (pt == typeof(DateTime?)) {
DateTime? dt = rs.getDate(name);
if (dt == DateTime.MinValue) dt = null;
pi.SetValue(o, dt, null);
} else if (pt == typeof(decimal)) {
decimal i = rs.getDecimal(name);
pi.SetValue(o, i, null);
} else if (pt == typeof(decimal?)) {
decimal? i = rs.getDecimal(name);
if (i == decimal.MinValue) i = null;
pi.SetValue(o, i, null);
}else if (pt == typeof(bool?)) {
bool? i = null;
var charBool = rs.getChar(name);
if (charBool != null) {
if (charBool == '0') {
i = false;
} else if (charBool == '1') {
i = true;
}
}
pi.SetValue(o, i, null);
}
}
return o;
}
Pex 探索在以下代码部分返回异常:
if (pt == typeof(string)) {
String s = rs.getString(name);
pi.SetValue(o, s, null);
}
我不明白为什么会得到:
System error: Index was outside the bounds of the array` exception.
我应该添加任何 PexAttribute 或 PexAssume 吗?你能帮忙吗?
“name”在“rs”中并且不等于 null 但问题在于:'pi.SetValue(o, s, null);'
最佳答案
唯一可能失败的行是 String s = rs.getString(name);
因为它正在访问一个数组。查看为什么 name
不在 rs 中 - 或者在询问之前验证它是否存在。
关于c# - 为什么在使用 pex 迭代 PropertyInfo 时出现 indexoutofboundsexception?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8154372/