为什么我得到indexoutofboundexception时迭代PropertyInfo使用pex

本文关键字:迭代 PropertyInfo 使用 pex indexoutofboundexception 为什么 | 更新日期: 2023-09-27 18:14:56

我有以下方法:

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);
}

我不明白为什么我得到:

系统错误:索引超出数组的异常范围。

我应该添加任何PexAttribute或PexAssume吗?你能帮帮我吗?

"name"在"rs"中,不等于null,但问题在行:'pi。SetValue(0, s, null);'

为什么我得到indexoutofboundexception时迭代PropertyInfo使用pex

唯一可能失败的行是String s = rs.getString(name);,因为它正在访问数组。查看为什么name不在rs中-或者在请求它之前验证它是否存在