private static void NonDefaultValidation() { var car = new Car { // Driver = new Driver(), MaxPassengers = 5 // WheelCount = 4 }; var steamTrain = new SteamTrain(); //{ // CarCount = 4 //}; var dieselTrain = new DieselTrain(); //{ // CarCount = 3 //}; ValidateEntity(car); }
private static void MaxValueValidation() { var train = new DieselTrain { CarCount = 100500 }; var properties = train.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public) .Where(info => info.PropertyType == typeof(int)) .Select(info => (PropertyInfo: info, MaxValueAttribute: info.GetCustomAttribute <MaxValueAttribute>())) .Where(tuple => tuple.MaxValueAttribute != null) .ToList(); properties.ForEach(tuple => { if ((int)tuple.PropertyInfo.GetValue(train) > tuple.MaxValueAttribute.MaxValue) { throw new ValidationException( $"Property {tuple.PropertyInfo.Name} must not have greater than {tuple.MaxValueAttribute.MaxValue}"); } }); Console.WriteLine($"Validation for {train.GetType().Name} passed!"); }