Beispiel #1
0
        private bool ReadColumns()
        {
            if (!_csv.Read())
            {
                return(false);
            }

            _columns = new string[_csv.FieldCount];
            if (_columns.Length < 1)
            {
                return(false);
            }

            for (int c = 0; c < _csv.FieldCount; c++)
            {
                _columns[c] = Regex.Replace(_csv.GetString(c), @"[^\w]", "_");
                if (ForceLowerCaseNames)
                {
                    _columns[c] = _columns[c].ToLowerInvariant();
                }
            }

            return(true);
        }
        /// <summary>
        /// Reads the next record from the file
        /// </summary>
        /// <returns></returns>
        public T Read()
        {
            // init if necessary
            if (!Initialized)
            {
                Initialize();
            }

            string sFixedLine = null;

            if (_csv != null)
            {
                if (!_csv.Read())
                {
                    return(null);
                }
            }
            else
            {
                sFixedLine = ReadLine();
                if (sFixedLine == null)
                {
                    return(null);
                }
            }

            var obj = new T();

            foreach (ITextField field in FieldArray)
            {
                string sValue;
                if (_csv != null)
                {
                    sValue = (field.Length > 0)
                                                                ? _csv.GetString(field.Position, field.Length).Trim()
                                                                : _csv.GetString(field.Position).Trim();
                }
                else if (sFixedLine != null)
                {
                    if (sFixedLine.Length <= field.Position)
                    {
                        continue;
                    }

                    sValue = (sFixedLine.Length > (field.Position + field.Length))
                                                ? sFixedLine.Substring(field.Position, field.Length).Trim()
                                                : sFixedLine.Substring(field.Position).Trim();
                }
                else
                {
                    continue;
                }

                if (string.IsNullOrEmpty(sValue))
                {
                    continue;
                }

                PropertyInfo pi       = field.Property;
                Type         propType = pi.PropertyType;
                if (propType.IsEnum)
                {
                    propType = Enum.GetUnderlyingType(propType);
                }

                try
                {
                    if (propType.IsAssignableFrom(typeof(String)))
                    {
                        pi.SetValue(obj, sValue, null);
                    }
                    else if (propType.IsAssignableFrom(typeof(Char)))
                    {
                        pi.SetValue(obj, Convert.ToChar(sValue), null);
                    }
                    else if (propType.IsAssignableFrom(typeof(Guid)))
                    {
                        pi.SetValue(obj, new Guid(sValue), null);
                    }
                    else if (propType.IsAssignableFrom(typeof(DateTime)))
                    {
                        pi.SetValue(obj, DateTime.Parse(sValue, null, field.DateTimeStyle), null);
                    }
                    else if (propType.IsAssignableFrom(typeof(Decimal)))
                    {
                        pi.SetValue(obj, Decimal.Parse(sValue, field.NumberStyle), null);
                    }
                    else if (propType.IsAssignableFrom(typeof(Double)))
                    {
                        pi.SetValue(obj, Double.Parse(sValue, field.NumberStyle), null);
                    }
                    else if (propType.IsAssignableFrom(typeof(Single)))
                    {
                        pi.SetValue(obj, Single.Parse(sValue, field.NumberStyle), null);
                    }
                    else if (propType.IsAssignableFrom(typeof(Int64)))
                    {
                        pi.SetValue(obj, Int64.Parse(sValue, field.NumberStyle), null);
                    }
                    else if (propType.IsAssignableFrom(typeof(Int32)))
                    {
                        pi.SetValue(obj, Int32.Parse(sValue, field.NumberStyle), null);
                    }
                    else if (propType.IsAssignableFrom(typeof(Int16)))
                    {
                        pi.SetValue(obj, Int16.Parse(sValue, field.NumberStyle), null);
                    }
                    else if (propType.IsAssignableFrom(typeof(Byte)))
                    {
                        pi.SetValue(obj, Byte.Parse(sValue, field.NumberStyle), null);
                    }
                    else if (propType.IsAssignableFrom(typeof(Boolean)))
                    {
                        pi.SetValue(obj, GetBoolean(sValue, field.BooleanTrue), null);
                    }
                    else
                    {
                        pi.SetValue(obj, Convert.ChangeType(sValue, pi.PropertyType), null);
                    }
                }
                catch (Exception ex)
                {
                    var s = string.Format("Unable to set Property `{0}` of Type `{1}` to value `{2}`", pi.Name, pi.PropertyType.Name, sValue);
                    throw new InvalidDataException(s, ex);
                }
            }

            return(obj);
        }