Esempio n. 1
0
 private static int contar(IFixedLenLine item)
 {
     if (item == null)
     {
         return(0);
     }
     return(1);
 }
Esempio n. 2
0
        public static string CalculaHashObjeto(IFixedLenLine Item)
        {
            using (var ms = new MemoryStream())
            {
                var sw = new StreamWriter(ms);
                Serializer.Serialize(Item, sw);
                string line = System.Text.Encoding.Default.GetString(ms.ToArray());

                return(Helpers.Hash.obterCrc32Utf8(line.Substring(0, line.Length - 10 - 2))); // tira o \r\n
            }
        }
Esempio n. 3
0
        public static bool VerificaHashObjeto(IFixedLenLine Item)
        {
            using (var ms = new MemoryStream())
            {
                var sw = new StreamWriter(ms);
                Serializer.Serialize(Item, sw);
                string line = System.Text.Encoding.Default.GetString(ms.ToArray());

                return(Helpers.Hash.Valida_NRControle(line.Substring(0, line.Length - 2),     // tira o \r\n
                                                      line.Substring(line.Length - 12, 10))); // tira o \r\n
            }
        }
Esempio n. 4
0
        public static void Serialize(this IFixedLenLine Object, StreamWriter stream)
        {
            var data = ReadObject(Object);

            foreach (var val in data)
            {
                string value = val.Formatter.Serialize(val);
                if (value.Length != val.Length)
                {
                    throw new InvalidOperationException("Serialized Length mismatch");
                }
                stream.Write(value);
            }
            stream.WriteLine();
            stream.Flush();
        }
Esempio n. 5
0
        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());
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        public static SerializationResult Deserialize(this IFixedLenLine Object, StreamReader stream)
        {
            var line = stream.ReadLine();

            return(Deserialize(Object, line));
        }