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 を追加しますか? 助けていただけますか?

「名前」は「rs」にあり、null と等しくありませんが、問題は「pi.SetValue(o, s, null);」にあります。

4

1 に答える 1

0

失敗する可能性のある唯一の行はString s = rs.getString(name);、配列にアクセスしているためです。が rs にない理由nameを確認するか、要求する前に存在することを確認してください。

于 2011-11-16T16:38:49.133 に答える