public bool IsValid(NotaExamen nota, out List <string> erori) { erori = new List <string>(); foreach (var property in nota.GetType().GetProperties()) { var attrs = property.GetCustomAttributes(true) .Where(a => typeof(RangeAttribute) .IsAssignableFrom(a.GetType())) .ToArray(); if (0 == attrs.Length) { continue; } var attr = (RangeAttribute)attrs[0]; //var actualValue = property.GetValue(nota); if (nota.Nota > attr.MaxValue) { erori.Add(string.Format(@"Nota e mai mare decat {0}", attr.MaxValue)); return(false); } if (nota.Nota < attr.MinValue) { erori.Add(string.Format(@"Nota e mai mica decat {0}", attr.MinValue)); return(false); } } return(true); }
static void Main(string[] args) { var note = new NotaExamen[] { new NotaExamen { Name = "Caligrafie", Nota = 10 } , new NotaExamen { Name = "Matematica", Nota = 2 } , new NotaExamen { Name = "Romana", Nota = -1 } }; var vvalidator = new NotaExamenValidator(); foreach (var nota in note) { List <string> errors; if (vvalidator.IsValid(nota, out errors)) { Console.WriteLine(string.Format(@"Exam {0} nota {1} este ok ", nota.Name, nota.Nota)); } else { Console.WriteLine(string.Format(@"Exam {0} nota {1} NUU este ok ", nota.Name, nota.Nota)); foreach (var error in errors) { Console.WriteLine(error); } } } Console.WriteLine(); var stringValidator = new StringStartsWithValidator(); foreach (var nota in note) { List <string> errors; if (stringValidator.StrtsWithAttribute(nota, out errors)) { Console.WriteLine(string.Format(@"Stringul {0} incepe cu stringul dorit!", nota.Name)); } else { Console.WriteLine(string.Format(@"Stringul {0} nu incepe cu stringul dorit!", nota.Name)); foreach (var error in errors) { Console.WriteLine(error); } } } Console.ReadLine(); }