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

            FieldInfo[] 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 (int i = 0; i < fields.Length; i++)
            {
                MappingInfo map = new MappingInfo(fields[i]);
                map.mDataColumnIndex = i;
                mMappings.Add(map);
            }


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

                for (int 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
        }