3

我有以下方法:

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(o, s, null);'

4

1 回答 1

0

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

于 2011-11-16T16:38:49.133 回答