public PropertyMapping MapProperty <TRecord>(Expression <Func <TRecord, object> > expression) { var property = GetProperty <TRecord>(expression); var propertyMap = new PropertyMapping(property) { MappedColumnIndex = getMaxIndex() + 1 }; _propertyMappings.Add(propertyMap); return(propertyMap); }
//private Dictionary<string, PropertyInfo> _propertyMappings = new Dictionary<string, PropertyInfo>(); private void AutoGeneratePropertyMappings <TRecord>() { if (_propertyMappings.Count == 0) { var properties = typeof(TRecord).GetProperties().Where(x => x.PropertyType.Module.ScopeName == "System.Private.CoreLib.dll").ToArray(); //var properties = typeof(TRecord).GetProperties(); if (FirstRowIsHeader) { AutoGeneratePropertyMappingsByName <TRecord>(properties); //get the propertied that were not mapped var headers = FieldHeaders.ToList <string>(); var mappedNames = _propertyMappings.Select(n => n.MappedColumnName).ToList(); foreach (var property in properties) { var existingPM = _propertyMappings.Where(m => m.PropertyInfo == property).FirstOrDefault(); if (existingPM != null) { continue; } for (int i = 0; i < FieldHeaders.Length; i++) { if (mappedNames.Contains(FieldHeaders[i])) { continue; } PropertyMapping pm = new PropertyMapping(property) { MappedColumnIndex = i, MappedColumnName = FieldHeaders[i], }; mappedNames.Add(FieldHeaders[i]); _propertyMappings.Add(pm); break; } } } else { AutoGeneratePropertyMappingsByIndex <TRecord>(properties); } } }
private void AutoGeneratePropertyMappingsByIndex <TRecord>(PropertyInfo[] properties) { foreach (var property in properties) { int index = getMaxIndex() + 1; if (index < CurrentRecord.Length) { PropertyMapping pm = new PropertyMapping(property) { MappedColumnIndex = index, MappedColumnName = FirstRowIsHeader ? FieldHeaders[index] : string.Empty }; _propertyMappings.Add(pm); } } }
private void AutoGeneratePropertyMappingsByName <TRecord>(PropertyInfo[] properties) { foreach (var property in properties) { int index = Array.FindIndex(FieldHeaders, t => t.Equals(property.Name, StringComparison.InvariantCultureIgnoreCase)); if (index == -1) { continue; } PropertyMapping pm = new PropertyMapping(property) { MappedColumnIndex = index, MappedColumnName = FieldHeaders[index] }; _propertyMappings.Add(pm); } }