internal static void PopulateObjectValue(EdiReader reader, EdiStructure structure, EdiPropertyDescriptor descriptor, bool read) { var cache = structure.CachedReads; var valueInfo = descriptor.ValueInfo; var text = cache.ContainsPath(valueInfo.Path) ? cache.ReadAsString(valueInfo.Path) : read?reader.ReadAsString() : (string)reader.Value; descriptor.Info.SetValue(structure.Instance, ConvertUtils.ConvertOrCast(text, reader.Culture, descriptor.Info.PropertyType)); }
internal static void PopulateStringValue(EdiReader reader, EdiStructure structure, EdiPropertyDescriptor descriptor, bool read) { var cache = structure.CachedReads; var valueInfo = descriptor.ValueInfo; var text = cache.ContainsPath(valueInfo.Path) ? cache.ReadAsString(valueInfo.Path) : read?reader.ReadAsString() : (string)reader.Value; descriptor.Info.SetValue(structure.Instance, text); }
internal static void PopulateBooleanValue(EdiReader reader, EdiStructure structure, EdiPropertyDescriptor descriptor, bool read) { var cache = structure.CachedReads; var valueInfo = descriptor.ValueInfo; var text = cache.ContainsPath(valueInfo.Path) ? cache.ReadAsString(valueInfo.Path) : read?reader.ReadAsString() : (string)reader.Value; if (!string.IsNullOrEmpty(text)) { int integerValue = default(int); bool booleanValue = default(bool); if (int.TryParse(text, NumberStyles.Integer, reader.Culture, out integerValue)) { booleanValue = integerValue == 1; } else if (bool.TryParse(text, out booleanValue)) { } else { throw new EdiException("Unable to convert string '{0}' to boolean.".FormatWith(reader.Culture, text)); } descriptor.Info.SetValue(structure.Instance, booleanValue); } }
internal static void PopulateCharValue(EdiReader reader, EdiStructure structure, EdiPropertyDescriptor descriptor, bool read) { var cache = structure.CachedReads; var valueInfo = descriptor.ValueInfo; var text = cache.ContainsPath(valueInfo.Path) ? cache.ReadAsString(valueInfo.Path) : read?reader.ReadAsString() : (string)reader.Value; if (!string.IsNullOrEmpty(text)) { if (text.Length > 1) { throw new EdiException("Unable to convert string '{0}' to char. It is more than 1 character long.".FormatWith(reader.Culture, text)); } descriptor.Info.SetValue(structure.Instance, text[0]); } }
internal static void PopulateInt32Value(EdiReader reader, EdiStructure structure, EdiPropertyDescriptor descriptor, bool read) { var cache = structure.CachedReads; var valueInfo = descriptor.ValueInfo; if (!descriptor.Info.PropertyType.IsEnum()) { var integer = cache.ContainsPath(valueInfo.Path) ? cache.ReadAsInt32(valueInfo.Path, reader.Culture) : read?reader.ReadAsInt32() : (int?)reader.Value; if (integer.HasValue) { descriptor.Info.SetValue(structure.Instance, ConvertUtils.ConvertOrCast(integer.Value, CultureInfo.InvariantCulture, descriptor.Info.PropertyType)); } } else { var enumValueString = cache.ContainsPath(valueInfo.Path) ? cache.ReadAsString(valueInfo.Path) : read?reader.ReadAsString() : (string)reader.Value; if (!string.IsNullOrEmpty(enumValueString)) { descriptor.Info.SetValue(structure.Instance, ConvertUtils.ConvertOrCast(enumValueString, CultureInfo.InvariantCulture, descriptor.Info.PropertyType)); } } }
internal static void PopulateDecimalValue(EdiReader reader, EdiStructure structure, EdiPropertyDescriptor descriptor, bool read) { var cache = structure.CachedReads; var valueInfo = descriptor.ValueInfo; var numberFloat = cache.ContainsPath(valueInfo.Path) ? cache.ReadAsDecimal(valueInfo.Path, descriptor.ValueInfo.Picture, reader.Grammar.DecimalMark) : read?reader.ReadAsDecimal(descriptor.ValueInfo.Picture) : (decimal?)reader.Value; if (numberFloat != null) { descriptor.Info.SetValue(structure.Instance, numberFloat); } }
internal static void PopulateDateTimeValue(EdiReader reader, EdiStructure structure, EdiPropertyDescriptor descriptor, bool read) { var cache = structure.CachedReads; var valueInfo = descriptor.ValueInfo; var dateString = cache.ContainsPath(valueInfo.Path) ? cache.ReadAsString(valueInfo.Path) : read?reader.ReadAsString() : (string)reader.Value; if (dateString != null) { dateString = dateString.Substring(0, valueInfo.Picture.Scale); var date = default(DateTime); if (dateString.TryParseEdiDate(valueInfo.Format, CultureInfo.InvariantCulture, out date)) { var existingDateObject = descriptor.Info.GetValue(structure.Instance); var existingDate = default(DateTime); if (existingDateObject != null && !existingDateObject.Equals(default(DateTime))) { if (existingDateObject is DateTime?) { existingDate = ((DateTime?)existingDateObject).Value; } else { existingDate = ((DateTime)existingDateObject); } if (date - date.Date == default(TimeSpan)) { date = date.Date.Add(existingDate - existingDate.Date); } else { date = existingDate.Add(date - date.Date); } } descriptor.Info.SetValue(structure.Instance, date); } } }