public T ReadObjectWithSeparatorChar(IDataRow row, AggregatedException ae) { T obj = new T(); for (int i = 0; i < row.Count; i++) { FileColumnAttribute tfi = m_Array[i]; string value = row[i].Value; Convert(obj, tfi, value, row[i].LineNbr, ae); } for (int i = row.Count; i < m_Array.Length; i++) { FileColumnAttribute tfi = m_Array[i]; /* if (((!m_fileDescription.EnforceCsvColumnAttribute) || * tfi.hasColumnAttribute) && * (!tfi.canBeNull)) * { * ae.AddException( * new MissingRequiredFieldException( * typeof(T).ToString(), * tfi.name, * row[row.Count - 1].LineNbr, * m_fileName)); * }*/ } return(obj); }
public WrongFormatException(string message, string typeName, string Value, FileColumnAttribute col, long LineNbr, string fileName) : base(message, null) { Data["TypeName"] = typeName; Data["FileName"] = fileName; Data["LineNbr"] = LineNbr; Data["Value"] = Value; Data["FileColumnAttribute"] = col.FieldIndex; }
private string ConvertObject(FileColumnAttribute col, Object objValue) { string resultString = null; if (objValue != null) { if ((objValue is IFormattable)) { resultString = ((IFormattable)objValue).ToString( col.OutputFormat, m_fileDescription.FileCultureInfo); if (col.WithOutSeparator) { resultString = resultString.Replace(m_fileDescription.FileCultureInfo.NumberFormat.CurrencyDecimalSeparator, ""); } if (resultString == null && !col.CanBeNull) { throw new Exception(string.Format("{0} is NULL, but this colunm Can't be NULL", col.Property)); } Alter(ref resultString, col, col.TextAlign == Align.None ? false : col.TextAlign == Align.Left); } else { resultString = objValue.ToString(); if (resultString == null && !col.CanBeNull) { throw new Exception(string.Format("{0} is NULL, but this colunm Can't be NULL", col.Property)); } Alter(ref resultString, col, col.TextAlign == Align.None ? true : col.TextAlign == Align.Left); } } else { if (!col.CanBeNull) { throw new Exception(string.Format("{0} is NULL, but this colunm Can't be NULL", col.Property)); } Alter(ref resultString, col, col.TextAlign == Align.None ? false : col.TextAlign == Align.Left); } return(resultString); }
private void Convert(T obj, FileColumnAttribute col, string value, int LineNbr, AggregatedException ae) { if (value == null || col.PassOver) { if (!col.CanBeNull) { ae.AddException(new WrongFormatException("Value can't be null", typeof(T).ToString(), value, col, LineNbr, m_fileName)); } //throw new Exception(); } else { if (value != null) { value = value.Trim(); if (col.MaxLength != UInt16.MaxValue && value.Length > col.MaxLength) { ae.AddException(new WrongFormatException(" Value too long", typeof(T).ToString(), value, col, LineNbr, m_fileName)); value = value.Substring(0, col.MaxLength); } } try { Object objValue = null; // Normally, either tfi.typeConverter is not null, // or tfi.parseNumberMethod is not null. // if (col.typeConverter != null) { objValue = col.typeConverter.ConvertFromString( null, m_fileDescription.FileCultureInfo, value); } else if (col.parseNumberMethod != null) { if (col.WithOutSeparator) { int pos = col.MaxLength - col.OutputFormat.IndexOf('.'); if (col.MaxLength == value.Length) { value = value.Substring(0, col.MaxLength - pos) + m_fileDescription.FileCultureInfo.NumberFormat.CurrencyDecimalSeparator + value.Substring(col.MaxLength - pos, pos); } else { if (value.Length > pos) { value = value.Substring(0, value.Length - pos) + m_fileDescription.FileCultureInfo.NumberFormat.CurrencyDecimalSeparator + value.Substring(value.Length - pos, pos); } else { ae.AddException(new WrongFormatException(" Value too long", typeof(T).ToString(), value, col, LineNbr, m_fileName)); return; //throw new Exception(); } } } objValue = col.parseNumberMethod.Invoke( col.fieldType, new Object[] { value, col.NumberStyle, m_fileDescription.FileCultureInfo }); } else if (col.parseDateMethod != null) { objValue = col.parseDateMethod.Invoke( col.fieldType, new Object[] { value, col.OutputFormat, m_fileDescription.FileCultureInfo }); } else { // No TypeConverter and no Parse method available. // Try direct approach. objValue = value; } if (col.MemberInfo != null) { if (col.MemberInfo is PropertyInfo) { ((PropertyInfo)col.MemberInfo).SetValue(obj, objValue, null); } else { ((FieldInfo)col.MemberInfo).SetValue(obj, objValue); } } else if (!m_fileDescription.AllowSkipColunm) { throw new Exception(); } } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ex.InnerException; } if (ex is FormatException) { ae.AddException(new WrongFormatException(value.ToString() + "-" + col.ToString() + "-" + LineNbr.ToString() + "Wrong Format", typeof(T).ToString(), value, col, LineNbr, m_fileName)); /*ex = new WrongDataFormatException( * typeof(T).ToString(), * tfi.name, * value, * row[i].LineNbr, * m_fileName, * ex);*/ } // ae. } } // return default(T); }
private void Alter(ref string resultString, FileColumnAttribute col, bool bLeft) { if (m_fileDescription.ValidChar != null) { m_fileDescription.ValidChar.Corrige(ref resultString); } if (resultString != null) { if (col.MaxLength == UInt16.MaxValue && m_fileDescription.UseMaxLenght) { throw new Exception(string.Format("{0} don't have a Maxlength, needed for a file without separator", col.Property)); } if (m_fileDescription.UseMaxLenght && resultString.Length < col.MaxLength) { if (bLeft) { resultString = resultString + new string(/*' '*/ col.FillChar, col.MaxLength - resultString.Length); } else { resultString = new string(col.FillChar, col.MaxLength - resultString.Length) + resultString; } } else { if (bLeft) { resultString = resultString.Length > col.MaxLength ? resultString.Substring(0, col.MaxLength) : resultString; } else { resultString = resultString.Length > col.MaxLength ? resultString.Substring(resultString.Length - col.MaxLength, col.MaxLength) : resultString; } } } else { resultString = new string(col.FillChar, col.MaxLength); } // if (!m_fileDescription.SeparatorChar.HasValue || (m_fileDescription.UseMaxLenght.HasValue && m_fileDescription.UseMaxLenght.Value)) // { // if (col.MaxLength == UInt16.MaxValue) // throw new Exception(string.Format("{0} don't have a Maxlength, needed for a file without separator", col.Property)); // if (resultString != null) // { // if (resultString.Length < col.MaxLength) // { // if (bLeft) // resultString = resultString + new string(/*' '*/col.FillChar , col.MaxLength - resultString.Length); // else // resultString = new string(col.FillChar, col.MaxLength - resultString.Length) + resultString; // } // else // resultString = resultString.Substring(0, col.MaxLength); // } // else // resultString = new string(col.FillChar, col.MaxLength); // } // else // if (col.MaxLength != UInt16.MaxValue) // if (resultString != null) // resultString = resultString.Length > col.MaxLength ? resultString.Substring(0, col.MaxLength) : resultString; }