Пример #1
0
        public IEnumerable <object> MapTableToObjects(Type type, string[][] table, ColumnIndexToPropertyNameMapping propertiesMapping)
        {
            var result = new List <object>();

            foreach (string[] row in table)
            {
                result.Add(row.MapToObjectProperties(type, propertiesMapping));
            }

            result.TrimExcess();
            return(result);
        }
Пример #2
0
        private ColumnIndexToPropertyNameMapping CreatePropertiesMapping(Type type, string[][] table)
        {
            if (this.numberOfRowsToSkip < 1)
            {
                throw new ApplicationException("This CSV is not supposed to have header row. Current method is not applicable in this case.");
            }

            var mappings = new ColumnIndexToPropertyNameMapping();

            var header       = table[0];
            int headerLength = header.Length;

            var properties = type.GetProperties()
                             .Where(p => Attribute.IsDefined(p, typeof(CsvColumnAttribute)));

            foreach (var property in properties)
            {
                string columnName = this.GetColumnNameInTable(property);
                if (!string.IsNullOrEmpty(columnName))
                {
                    int columnNumber = -1;
                    for (int i = 0; i < headerLength; ++i)
                    {
                        if (string.Compare(header[i], columnName) == 0)
                        {
                            columnNumber = i;
                            break;
                        }
                    }

                    if (columnNumber >= 0)
                    {
                        mappings.Mapping.Add(property.Name, columnNumber);
                    }
                }
            }

            return(mappings);
        }