Exemplo n.º 1
0
        private TypeFieldInfo AnalyzeTypeField(
                                MemberInfo mi,
                                bool allRequiredFieldsMustHaveFieldIndex,
                                bool allCsvColumnFieldsMustHaveFieldIndex)
        {
            TypeFieldInfo tfi = new TypeFieldInfo();
            tfi.MemberInfo = mi;
            tfi.UpdateParseParameters(FileDescription.UseOutputFormatForParsingCsvValue);
            tfi.UpdateAttributes();
            tfi.ValidateAttributes<T>(allCsvColumnFieldsMustHaveFieldIndex, allRequiredFieldsMustHaveFieldIndex);

            return tfi;
        }
Exemplo n.º 2
0
        public void WriteObject(T obj, List<string> row)
        {
            row.Clear();
            
            for (int i = 0; i < fieldIndexInfo.IndexToInfo.Length; i++)
            {
                TypeFieldInfo tfi = fieldIndexInfo.IndexToInfo[i];

                if (FileDescription.EnforceCsvColumnAttribute &&
                        (!tfi.HasColumnAttribute))
                {
                    continue;
                }

                Object objValue;


                if (tfi.MemberInfo is PropertyInfo)
                {
                    objValue =
                        ((PropertyInfo)tfi.MemberInfo).GetValue(obj, null);
                }
                else
                {
                    objValue =
                        ((FieldInfo)tfi.MemberInfo).GetValue(obj);
                }

                string resultString = null;
                if (objValue != null)
                {
                    if ((objValue is IFormattable))
                    {
                        resultString =
                            ((IFormattable)objValue).ToString(
                                tfi.OutputFormat,
                                FileDescription.FileCultureInfo);
                    }
                    else
                    {
                        resultString = objValue.ToString();
                    }
                }

                row.Add(resultString);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Writes the field names given in T to row.
        /// </summary>
        /// 
        public void WriteNames(List<string> row)
        {
            row.Clear();

            for (int i = 0; i < fieldIndexInfo.IndexToInfo.Length; i++)
            {
                TypeFieldInfo tfi = fieldIndexInfo.IndexToInfo[i];

                if (FileDescription.EnforceCsvColumnAttribute &&
                        (!tfi.HasColumnAttribute))
                {
                    continue;
                }

                row.Add(tfi.Name);
            }
        }
Exemplo n.º 4
0
        private void InitializeNameToInfo(Type type, bool allRequiredFieldsMustHaveFieldIndex, bool allCsvColumnFieldsMustHaveFieldIndex)
        {
            nameToInfo.Clear();

            foreach (MemberInfo mi in type.GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
            {
                // Only process field and property members.
                if ((mi.MemberType == MemberTypes.Field) ||
                    (mi.MemberType == MemberTypes.Property))
                {
                    // Note that the compiler does not allow fields and/or properties
                    // with the same name as some other field or property.
                    TypeFieldInfo tfi =
                        AnalyzeTypeField(mi,
                                allRequiredFieldsMustHaveFieldIndex,
                                allCsvColumnFieldsMustHaveFieldIndex);
                    nameToInfo[tfi.Name] = tfi;
                }
            }
        }
Exemplo n.º 5
0
        public T ReadObject(IDataRow row, AggregatedException ae)
        {
            if (row.Count > fieldIndexInfo.IndexToInfo.Length)
            {
                //Are we ignoring unknown columns?
                if (!FileDescription.IgnoreUnknownColumns)
                {
                    // Too many fields
                    throw new TooManyDataFieldsException(typeof(T).ToString(), row[0].LineNbr, FileName);
                }
            }

            T obj = new T();

            //If we will be using the mappings, we just iterate through all the cells in this row
            int maxRowCount = fieldIndexInfo.GetMaxRowCount(row.Count);


            for (int i = 0; i < maxRowCount; i++)
            {
                TypeFieldInfo tfi = fieldIndexInfo.QueryTypeFieldInfo(FileDescription.IgnoreUnknownColumns, i);
                if (tfi == null)
                {
                    continue;
                }

                if (FileDescription.EnforceCsvColumnAttribute &&
                    (!tfi.HasColumnAttribute))
                {
                    // enforcing column attr, but this field/prop has no column attr.
                    // So there are too many fields in this record.
                    throw new TooManyNonCsvColumnDataFieldsException(typeof(T).ToString(), row[i].LineNbr, FileName);
                }

                if ((!FileDescription.FirstLineHasColumnNames) &&
                    (tfi.Index == ColumnAttribute.McDefaultFieldIndex))
                {
                    // First line in the file does not have field names, so we're
                    // depending on the FieldIndex of each field in the type
                    // to ensure each value is placed in the correct field.
                    // However, now hit a field where there is no FieldIndex.
                    throw new MissingFieldIndexException(typeof(T).ToString(), row[i].LineNbr, FileName);
                }

                if (FileDescription.UseFieldIndexForReadingData && (!FileDescription.FirstLineHasColumnNames) &&
                    (tfi.Index > row.Count))
                {
                    // First line in the file does not have field names, so we're
                    // depending on the FieldIndex of each field in the type
                    // to ensure each value is placed in the correct field.
                    // However, now hit a field where the FieldIndex is bigger
                    // than the total number of items in a row generated by the separatorChar
                    throw new WrongFieldIndexException(typeof(T).ToString(), row[i].LineNbr, FileName);
                }


                int index = FileDescription.UseFieldIndexForReadingData ? tfi.Index - 1 : i;

                // value to put in the object
                string value = row[index].Value;

                if (value == null)
                {
                    if (!tfi.CanBeNull)
                    {
                        ae.AddException(new MissingRequiredFieldException(
                                            typeof(T).ToString(), tfi.Name, row[i].LineNbr, FileName));
                    }
                }
                else
                {
                    try
                    {
                        if (tfi.OutputFormat == "HH:mm:ss.fff")
                        {
                            value = value.Substring(0, 8) + "." + value.Substring(9);
                        }
                        Object objValue = tfi.UpdateObjectValue(value, FileDescription.FileCultureInfo);

                        if (tfi.MemberInfo is PropertyInfo)
                        {
                            ((PropertyInfo)tfi.MemberInfo).SetValue(obj, objValue, null);
                        }
                        else
                        {
                            ((FieldInfo)tfi.MemberInfo).SetValue(obj, objValue);
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is TargetInvocationException)
                        {
                            e = e.InnerException;
                        }

                        if (e is FormatException)
                        {
                            e = new WrongDataFormatException(typeof(T).ToString(), tfi.Name,
                                                             value, row[i].LineNbr, FileName, e);
                        }
                        ae.AddException(e);
                    }
                }
            }

            // Visit any remaining fields in the type for which no value was given
            // in the data row, to see whether any of those was required.
            // If only looking at fields with CsvColumn attribute, do ignore
            // fields that don't have that attribute.
            for (int i = row.Count; i < fieldIndexInfo.IndexToInfo.Length; i++)
            {
                TypeFieldInfo tfi = fieldIndexInfo.IndexToInfo[i];

                if (((!FileDescription.EnforceCsvColumnAttribute) || tfi.HasColumnAttribute) &&
                    (!tfi.CanBeNull))
                {
                    ae.AddException(new MissingRequiredFieldException(typeof(T).ToString(), tfi.Name,
                                                                      row[row.Count - 1].LineNbr, FileName));
                }
            }
            return(obj);
        }