Почему я получаю indexoutofboundsexception при итерации PropertyInfo с использованием pex? - PullRequest
3 голосов
/ 16 ноября 2011

У меня есть следующий метод:

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" и не равно нулю, но проблема в строке: 'pi.SetValue (o, s, null);'

1 Ответ

0 голосов
/ 16 ноября 2011

Единственная строка, которая может быть неудачной - это String s = rs.getString(name);, так как она обращается к массиву.Узнайте, почему name не указан в rs - или убедитесь, что он существует, прежде чем запрашивать его.

...