示例#1
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="input"></param>
        public Mapper(object input)
            : this()
        {
            var reader = input as IDataReader;

            if (reader != null)
            {
                _reader = new DataReaderEnumerator(reader);
                Error   = _reader.Error;
                _index  = 1;
                return;
            }

            var no = input as NameObjectCollectionBase;

            if (no != null)
            {
                _nv    = new NameValueEnumerator(no);
                Error  = _nv.Error;
                _index = 2;
                return;
            }

            var row = (input as DataRowView)?.Row ?? input as DataRow;

            if (row?.Table != null)
            {
                _row   = new DataRowEnumerator(row);
                Error  = _row.Error;
                _index = 3;
                return;
            }

            var dataset = input as DataSet;

            if (dataset != null)
            {
                _dataSet = new DataSetEnumerator(dataset);
                Error    = _dataSet.Error;
                _index   = 4;
                return;
            }

            var dict = input as IDictionary;

            if (dict != null)
            {
                _enumerator = dict.GetEnumerator();
                _index      = 5;
                return;
            }

            var ee = (input as IEnumerable)?.GetEnumerator() ?? input as IEnumerator;

            if (ee != null)
            {
                _pair  = new PairEnumerator(ee);
                Error  = _pair.Error;
                _index = 6;
                return;
            }

            var ps = PublicPropertyCache.GetByType(input.GetType());

            if (ps.Length > 0)
            {
                _property = new PropertyEnumerator(input, ps);
                Error     = _property.Error;
                _index    = 7;
            }
        }
示例#2
0
        public void TestEnumerator()
        {
            ExceptionAssert.Throws <ArgumentNullException>(() => new DataSetEnumerator(null));

            DataSet dataSet = new DataSet();

            ExceptionAssert.Throws <InvalidOperationException>(() => new DataSetEnumerator(dataSet), "DataSet does not contain any table");

            DataTable dataTable1 = new DataTable("Table1");

            dataTable1.Columns.Add("Column", typeof(int));

            dataTable1.Rows.Add(1);
            dataTable1.Rows.Add(2);
            dataTable1.Rows.Add(3);

            DataTable dataTable2 = new DataTable("Table2");

            dataTable2.Columns.Add("Column", typeof(int));

            dataTable2.Rows.Add(111);
            dataTable2.Rows.Add(222);
            dataTable2.Rows.Add(333);

            dataSet.Tables.Add(dataTable1);
            dataSet.Tables.Add(dataTable2);
            ExceptionAssert.Throws <InvalidOperationException>(() => new DataSetEnumerator(dataSet, "BadTable"), "DataSet does not contain table with name \"BadTable\"");

            IList <int> result     = new List <int>(dataTable1.Rows.Count);
            var         enumerator = new DataSetEnumerator(dataSet);

            Assert.IsNull(enumerator.Current);
            Assert.IsNull(enumerator.Current);
            while (enumerator.MoveNext())
            {
                result.Add((int)enumerator.Current.ItemArray[0]);
            }

            Assert.IsFalse(enumerator.MoveNext());
            Assert.IsFalse(enumerator.MoveNext());

            Assert.AreEqual(3, enumerator.Current.ItemArray[0]);
            Assert.AreEqual(3, enumerator.Current.ItemArray[0]);

            Assert.AreEqual(3, result.Count);
            Assert.AreEqual(1, result[0]);
            Assert.AreEqual(2, result[1]);
            Assert.AreEqual(3, result[2]);

            enumerator.Reset();
            result.Clear();

            enumerator = new DataSetEnumerator(dataSet, "Table2");
            Assert.IsNull(enumerator.Current);
            Assert.IsNull(enumerator.Current);
            while (enumerator.MoveNext())
            {
                result.Add((int)enumerator.Current.ItemArray[0]);
            }

            Assert.IsFalse(enumerator.MoveNext());
            Assert.IsFalse(enumerator.MoveNext());

            Assert.AreEqual(333, enumerator.Current.ItemArray[0]);
            Assert.AreEqual(333, enumerator.Current.ItemArray[0]);
            Assert.AreEqual(3, result.Count);
            Assert.AreEqual(111, result[0]);
            Assert.AreEqual(222, result[1]);
            Assert.AreEqual(333, result[2]);
        }