public static SerializableValue[] ReadObject(this IFixedLenLine Object) { var myType = Object.GetType(); IList <PropertyInfo> props = new List <PropertyInfo>(myType.GetProperties()); var dicProps = new Dictionary <int, SerializableValue>(); foreach (var prop in props) { if (prop.GetCustomAttributes(typeof(IgnoreAttribute), true).Any()) { continue; } TipoRegistro tipo = TipoRegistro.Skip; var len = checkProperty <LengthAttribute>(myType, prop); tipo = checkProperty <TypeAttribute>(myType, prop).Tipo; var index = checkProperty <IndexAttribute>(myType, prop); if (dicProps.ContainsKey(index.Index)) { throw new InvalidOperationException("IndexAttribute can not be duplicate " + myType.Name + "." + prop.Name + ", Index:" + index.Index); } object propVal = prop.GetValue(Object); var formatter = Formatter.GetNativeFormatter(tipo, prop.PropertyType, propVal); if (formatter == null) { throw new InvalidOperationException("None Formatter matched " + myType.Name + "." + prop.Name + ", Index:" + index.Index); } dicProps.Add(index.Index, new SerializableValue() { Name = prop.Name, Index = index.Index, Length = len.Length, Decimals = len.Decimals, Tipo = tipo, Object = propVal, Type = prop.PropertyType, Formatter = formatter, }); } return(dicProps.OrderBy(x => x.Key).Select(x => x.Value).ToArray()); }
public static SerializationResult Deserialize(this IFixedLenLine Object, string line) { var serResult = new SerializationResult(); var data = ReadObject(Object); for (int idx = data.Min(o => o.Index); idx <= data.Max(o => o.Index); idx++) { if (!data.Select(o => o.Index).Contains(idx)) { //throw new InvalidOperationException("Index " + idx + " is missing"); } } int offset = 0; foreach (var val in data) { try { if (val.Formatter.Deserialize(line, offset, val)) { Object.GetType().GetProperty(val.Name).SetValue(Object, val.Object); } else { serResult.Errors.Add(new Exception("Failed to deserialize field " + val.Name)); } } catch (Exception ex) { serResult.Errors.Add(new Exception("Error serializing field " + val.Name, ex)); } offset += val.Length; } return(serResult); }