Ejemplo n.º 1
0
        public T[] AutoMapDataTable2RecordsByIndex(DataTable dt)
        {
            List <T> arr = new List <T>(dt.Rows.Count);
#endif

            var fields =
                mRecordInfo.mRecordType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField |
                                                  BindingFlags.Instance | BindingFlags.IgnoreCase);


            if (fields.Length > dt.Columns.Count)
            {
                throw new FileHelpersException("The data table has less fields than fields in the Type: " +
                                               mRecordInfo.mRecordType.Name);
            }

            for (var i = 0; i < fields.Length; i++)
            {
                var map = new MappingInfo(fields[i]);
                map.mDataColumnIndex = i;
                mMappings.Add(map);
            }


            foreach (DataRow row in dt.Rows)
            {
#if !GENERICS
                var record = mRecordInfo.CreateRecordObject();
#else
                T record = (T)mRecordInfo.CreateRecordObject();
#endif
                //TypedReference t = TypedReference.MakeTypedReference(record, new FieldInfo[]) null);

                for (var i = 0; i < mMappings.Count; i++)
                {
                    ((MappingInfo)mMappings[i]).DataToField(row, record);
                }

                arr.Add(record);
            }

#if !GENERICS
            return((object[])arr.ToArray(mRecordInfo.mRecordType));
#else
            return(arr.ToArray());
#endif
        }