Esempio n. 1
0
        private void SetPropertyValue(TRow row, ICsvColumn <TRow> column, CsvLine line, string element)
        {
            PropertyInfo prop = typeof(TRow).GetProperty(column.PropertyName);

            if (!this.Definition.IgnoreReadonlyProperties && !prop.CanWrite)
            {
                throw new CsvStreamMapperException("The field '" + column.PropertyName + "' is readonly (e.g. is not writable).", line);
            }
            if (prop.CanWrite)
            {
                prop.SetValue(row, Conversion.AsValue(prop.PropertyType, element, Definition.FormattingCulture), null);
            }
        }
Esempio n. 2
0
        private void SetPropertyValues(CsvLine line, TRow row)
        {
            foreach (var column in this.Definition.Columns)
            {
                ////TODO: Add support for composite columns, now the first is used
                if (column.GetReadingHeaderNames().Count() == 1)
                {
                    foreach (var header in column.GetReadingHeaderNames())
                    {
                        var element = GetValue(line, header);

                        try
                        {
                            SetPropertyValue(row, column, line, element);
                        }
                        catch (NullReferenceException e)
                        {
                            var message = "The value of '" + header + "' (" + column.PropertyName + ") was NULL on line " + line.SourceLine.LineNumber + ".";
                            line.SourceLine.ErrorMessages.Add(message);

                            if (Definition.ThrowExceptionOnError)
                            {
                                throw new CsvStreamMapperException(message, e, line);
                            }
                        }
                        catch (ArgumentException e)
                        {
                            var message = "The value of '" + header + "' (" + column.PropertyName + ") was invalid on line " + line.SourceLine.LineNumber + ".";
                            line.SourceLine.ErrorMessages.Add(message);

                            if (Definition.ThrowExceptionOnError)
                            {
                                throw new CsvStreamMapperException(message, e, line);
                            }
                        }
                        catch (Exception e)
                        {
                            var message = "An error ocurred in field '" + header + "' (" + column.PropertyName + ") on line " + line.SourceLine.LineNumber + ".";
                            line.SourceLine.ErrorMessages.Add(message);

                            if (Definition.ThrowExceptionOnError)
                            {
                                throw new CsvStreamMapperException(message, e, line);
                            }
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        private string GetValue(CsvLine line, Header header)
        {
            try
            {
                return(line[header]);
            }
            catch (KeyNotFoundException exception)
            {
                var message = "The corresponding value of header name '" + header + "' was not found in the collection on line " + line.SourceLine.LineNumber + ".";
                line.SourceLine.ErrorMessages.Add(message);

                if (Definition.ThrowExceptionOnError)
                {
                    throw new CsvStreamMapperException(message, exception, line);
                }
            }

            return(null);
        }
Esempio n. 4
0
        private CsvLine CreateDictionaryLine(CsvSourceLine line)
        {
            var indexedLine = new CsvLine(line);

            for (int index = 0; index < this.headers.Length; index++)
            {
                var header = this.headers.GetHeaderName(index);

                if (indexedLine.ContainsKey(header))
                {
                    line.ErrorMessages.Add($"A duplicate header name was found '{header}' on line {line.LineNumber}.");

                    if (definition.ThrowExceptionOnError)
                    {
                        throw new DuplicateHeaderException(header.Name, line.LineNumber);
                    }
                }

                indexedLine.Add(header, GetElementValue(line, index));
            }

            return(indexedLine);
        }
 /// <summary>Initializes a new instance of the <see cref="CsvStreamMapperException" /> class.</summary>
 /// <param name="message">The message.</param>
 /// <param name="innerException">The inner exception.</param>
 /// <param name="line">The line.</param>
 public CsvStreamMapperException(string message, Exception innerException, CsvLine line) : base(message, innerException)
 {
     this.Line = line;
 }
Esempio n. 6
0
 public CsvLine(CsvLine line, TRow row)
 {
     this.line = line;
     this.Row  = row;
 }