示例#1
0
        private bool CheckAndProcessEnumerables(CSVSettingsDataRecord settings, PropertyInfo propertyInfo, object dataRecord)
        {
            if (CheckIfIEnumerable(propertyInfo))
            {
                var dataRecordEnumerable = propertyInfo.GetValue(dataRecord);

                if (dataRecordEnumerable == null)
                {
                    dataRecordEnumerable = CreateIEnumerableInstance(propertyInfo.PropertyType);
                    propertyInfo.SetValue(dataRecord, dataRecordEnumerable);
                }

                settings.DataRecord     = null;
                settings.DataRecordType = propertyInfo.PropertyType.GetGenericArguments()[0];

                var childDataRecord = ProcessDataRowForDataRecordType(settings) as IEmpty;

                if (!(childDataRecord?.IsEmpty ?? false))
                {
                    dataRecordEnumerable.GetType().GetMethod("Add").Invoke(dataRecordEnumerable, new[] { childDataRecord });
                }

                return(true);
            }

            return(false);
        }
示例#2
0
        private T ProcessDataRow <T>(CSVSettings settings) where T : class
        {
            var dataCells = GetDataCells(settings);

            var dataRecordSettings = new CSVSettingsDataRecord
            {
                ColumnNames    = settings.ColumnNames,
                Properties     = settings.Properties,
                DataCells      = dataCells,
                DataRecord     = null,
                DataRecordType = typeof(T),
                CultureInfo    = settings.CultureInfo
            };

            return(ProcessDataRowForDataRecordType(dataRecordSettings) as T);
        }
示例#3
0
        private bool CheckAndProcessDefaultColumnIndex(CSVSettingsDataRecord settings, DataProperty propertyTarget, PropertyInfo propertyInfo)
        {
            if ((!(propertyTarget.DataProperties?.Any() ?? false) || !CheckIfClass(propertyInfo)) && !CheckIfIEnumerable(propertyInfo))
            {
                return(false);
            }

            var dataRecordSettings = new CSVSettingsDataRecord
            {
                ColumnNames = settings.ColumnNames,
                Properties  = propertyTarget.DataProperties,
                DataCells   = settings.DataCells,
                CultureInfo = settings.CultureInfo
            };

            return(CheckAndProcessEnumerables(dataRecordSettings, propertyInfo, settings.DataRecord) || CheckAndProcessDataClasses(dataRecordSettings, propertyInfo, settings.DataRecord));
        }
示例#4
0
        private bool CheckAndProcessDataClasses(CSVSettingsDataRecord settings, PropertyInfo propertyInfo, object dataRecord)
        {
            if (CheckIfClass(propertyInfo))
            {
                settings.DataRecord     = propertyInfo.GetValue(dataRecord);
                settings.DataRecordType = propertyInfo.PropertyType;

                var childDataRecord = ProcessDataRowForDataRecordType(settings);

                if (childDataRecord != null)
                {
                    propertyInfo.SetValue(dataRecord, childDataRecord);
                }

                return(true);
            }

            return(false);
        }
示例#5
0
        private object ProcessDataRowForDataRecordType(CSVSettingsDataRecord settings)
        {
            if (settings.DataRecord == null)
            {
                settings.DataRecord = Activator.CreateInstance(settings.DataRecordType);
            }

            var propertyInfos = settings.DataRecord.GetType().GetProperties();

            foreach (var propertyInfo in propertyInfos)
            {
                var propertyTargets = settings.Properties.Where(p => p.PropertyTarget.Equals(propertyInfo.Name));

                if (!propertyTargets.Any())
                {
                    continue;
                }

                ProcessPropertyInfo(propertyTargets, propertyInfo, settings);
            }

            return(settings.DataRecord);
        }
示例#6
0
        private void ProcessPropertyInfo(IEnumerable <DataProperty> propertyTargets, PropertyInfo propertyInfo, CSVSettingsDataRecord settings)
        {
            foreach (var propertyTarget in propertyTargets)
            {
                if (propertyTarget.HasMapping)
                {
                    SetPropertyValue(propertyInfo, settings.DataRecord, propertyTarget.MappedValue, settings.CultureInfo);
                }
                else
                {
                    var columnIndex = settings.ColumnNames.ContainsKey(propertyTarget.PropertySource ?? "") ? settings.ColumnNames[propertyTarget.PropertySource] : -1;

                    if (columnIndex >= settings.DataCells.Length)
                    {
                        continue;
                    }

                    if (columnIndex == -1)
                    {
                        CheckAndProcessDefaultColumnIndex(settings, propertyTarget, propertyInfo);

                        continue;
                    }

                    var rawValue = settings.DataCells[columnIndex];
                    if (propertyTarget.ValueMappings != null)
                    {
                        var mapping = propertyTarget.ValueMappings.FirstOrDefault(m =>
                                                                                  (m.Source.IsNullOrEmpty() && rawValue.IsNullOrEmpty()) ||
                                                                                  (!m.Source.IsNullOrEmpty() && m.Source.Equals(rawValue, StringComparison.InvariantCultureIgnoreCase))
                                                                                  );

                        if (mapping != default && !mapping.Source.IsNullOrEmpty())
                        {
                            rawValue = mapping.Target;
                        }
                    }

                    SetPropertyValue(propertyInfo, settings.DataRecord, rawValue, settings.CultureInfo);
                }
            }
        }