c# - 为什么在使用 pex 迭代 PropertyInfo 时出现 indexoutofboundsexception?

标签 c# pex

我有以下方法:

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/

相关文章:

c# - 如何以编程方式退出或关闭 UWP 应用程序? (Windows 10)

c# - 为什么在呈现局部 View 之前不运行 _PageStart.cshtml?

c# - SortedList<> 及其奇怪的 setter

c# - 不在右键菜单中运行 Pex

tdd - 魔灵系统.dll

c# - 在 UWP 中检测空闲用户

c# - AutoComplete 是否适用于 WPF?

pex - 运行 PEX 时出现错误

windows-phone-7 - Pex 是否支持 windows phone 7 单元测试?

.net - 当单元测试调试停止时,Moles 崩溃