public Empregado(string line, int lineNumber) { Type classType = typeof(Empregado); PropertyInfo propInfo = null; EmpregadoAttribute attr = null; try { foreach (PropertyInfo propertyInfo in this.GetType().GetProperties()) { propInfo = propertyInfo; EmpregadoAttribute attribute = (EmpregadoAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(EmpregadoAttribute)); attr = attribute; string strValue = line.Substring(attribute.InitialPosition, attribute.Length).Trim(); if (propertyInfo.PropertyType.Name == nameof(DateTime)) { if (strValue == "00/00/0000") { propertyInfo.SetValue(this, DateTime.MinValue); } else { int year = int.Parse(strValue.Substring(6, 4)); int month = int.Parse(strValue.Substring(3, 2)); int day = int.Parse(strValue.Substring(0, 2)); propertyInfo.SetValue(this, new DateTime(year, month, day)); } } else if (propertyInfo.PropertyType.Name == nameof(Int32)) { if (!int.TryParse(strValue, out int intValue)) { throw new FormatException(string.Format("O valor \"{0}\" não pode ser convertido em um tipo inteiro válido.", strValue)); } propertyInfo.SetValue(this, intValue); } else if (propertyInfo.PropertyType.Name == nameof(Char)) { propertyInfo.SetValue(this, strValue[0]); } else { propertyInfo.SetValue(this, strValue); } } } catch (Exception ex) { Console.WriteLine(string.Format( "Um erro ocorreu ao processar a linha {0}; campo \"{1}\" (posição inicial {2})\nErro Técnico: {3}", lineNumber, propInfo.Name, (attr.InitialPosition + 1).ToString(), ex.Message)); } }
public Empregado(string line, int lineNumber, string fileName) { Type classType = typeof(Empregado); PropertyInfo propInfo = null; EmpregadoAttribute attr = null; try { foreach (PropertyInfo propertyInfo in this.GetType().GetProperties()) { propInfo = propertyInfo; EmpregadoAttribute attribute = (EmpregadoAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(EmpregadoAttribute)); attr = attribute; string strValue = line.Substring(attribute.InitialPosition, attribute.Length).Trim(); if (propertyInfo.PropertyType.Name == nameof(DateTime)) { if (strValue == "00/00/0000") { propertyInfo.SetValue(this, DateTime.MinValue); } else { int year = int.Parse(strValue.Substring(6, 4)); int month = int.Parse(strValue.Substring(3, 2)); int day = int.Parse(strValue.Substring(0, 2)); propertyInfo.SetValue(this, new DateTime(year, month, day)); } } else if (propertyInfo.PropertyType.Name == nameof(Int32)) { if (!int.TryParse(strValue, out int intValue)) { throw new FormatException(string.Format("O valor \"{0}\" não pode ser convertido em um tipo inteiro válido.", strValue)); } propertyInfo.SetValue(this, intValue); } else if (propertyInfo.PropertyType.Name == nameof(Char)) { propertyInfo.SetValue(this, strValue[0]); } else { propertyInfo.SetValue(this, strValue); } } } catch (Exception ex) { Console.WriteLine(string.Format( "Um erro ocorreu ao processar a linha {0}; campo \"{1}\" (posição inicial {2})\nErro Técnico: {3}", lineNumber, propInfo.Name, (attr.InitialPosition + 1).ToString(), ex.Message)); var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>(); optionsBuilder.UseSqlServer("Server=tcp:hackathonvalia.database.windows.net,1433;Initial Catalog=DesafioValia;Persist Security Info=False;User ID=firjan;Password=!@#123qweasdzxc;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"); using (var _context = new ApplicationDbContext(optionsBuilder.Options)) { // do stuff Arquivo arquivo = _context.Arquivos.FirstOrDefault(a => a.NomeArquivo.Equals(fileName)); if (arquivo == null) { throw new NullReferenceException("O arquivo " + fileName + " não foi localizado"); } Erros erro = new Erros { Campo = propInfo.Name, PosicaoInicial = attr.InitialPosition + 1, PosicaoFinal = ((attr.InitialPosition + 1) + attr.Length) - 1, Linha = lineNumber, Texto = ex.ToString(), Tamanho = attr.Length, ArquivoId = 1 }; _context.Erros.Add(erro); _context.SaveChanges(); //string originPath = Path.Combine(@"D:\OneDrive\Novo", fileName + "111.txt"); //string destinationPath = Path.Combine(@"D:\OneDrive\Parcialmentebemsucedido", fileName + "111.txt"); //File.Move(originPath, destinationPath); } } }