/// <summary> /// Populate a row's data into an instance of the provided class type /// </summary> /// <typeparam name="T">Type of class to generate</typeparam> /// <param name="rowIndex">Index of the row (not line number!) to populate</param> /// <returns>Instance of class</returns> private T Populate <T>(int rowIndex, PropertyFieldMappingInfo[] classMap) where T : class, new() { T instance = new T(); foreach (DataColumn column in ROWS[rowIndex].Columns) { PropertyFieldMappingInfo map = classMap[column.Index]; if (map != null) { map.Write(instance, column.Value); } } return(instance); }
/// <summary> /// Create the FIELD_PROPERTY_MAP /// </summary> /// <typeparam name="ClassT">Type of the class object we need to deserialize into</typeparam> private PropertyFieldMappingInfo[] MapHeaderToClass <ClassT>() where ClassT : class, new() { ClassInfo info = TypeInspector.InspectOnlyMembersAnotated <ClassT, FileFieldAttribute>(); if (info == null) { throw new InvalidOperationException($"The type '{typeof(ClassT).Name}' contains no usable properties/fields. Ensure the properties/fields are decorated with the 'FileFieldAttribute' attribute."); } List <PropertyFieldMappingInfo> tempList = new List <PropertyFieldMappingInfo>(); int maxColumnIndex = -1; for (int i = 0; i < info.Fields.Count; i++) { PropertyFieldMappingInfo map = new PropertyFieldMappingInfo(info.Fields[i]); if ((map.ColumnIndex < 0) && (HEADER != null)) { foreach (DataColumn col in HEADER.Columns) { if (col.Value == map.Attribute.Name) { map.ColumnIndex = col.Index; break; } } } if (map.ColumnIndex >= 0) { if (map.ColumnIndex > maxColumnIndex) { maxColumnIndex = map.ColumnIndex; } tempList.Add(map); } } for (int i = 0; i < info.Properties.Count; i++) { PropertyFieldMappingInfo map = new PropertyFieldMappingInfo(info.Properties[i]); if ((map.ColumnIndex < 0) && (HEADER != null)) { foreach (DataColumn col in HEADER.Columns) { if (col.Value == map.Attribute.Name) { map.ColumnIndex = col.Index; break; } } } if (map.ColumnIndex >= 0) { if (map.ColumnIndex > maxColumnIndex) { maxColumnIndex = map.ColumnIndex; } tempList.Add(map); } } if (tempList.Count == 0) { throw new InvalidOperationException($"The type '{typeof(ClassT).Name}' contains no usable properties/fields. Ensure the properties/fields are decorated with the 'FileFieldAttribute' attribute."); } PropertyFieldMappingInfo[] FIELD_PROPERTY_MAP = new PropertyFieldMappingInfo[maxColumnIndex + 1]; foreach (PropertyFieldMappingInfo item in tempList) { FIELD_PROPERTY_MAP[item.ColumnIndex] = item; } return(FIELD_PROPERTY_MAP); }