using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; namespace ex_ValidationWithAttributes { public class Person : IDataErrorInfo { public string Name { get; set; } /// /// Le prénom est requis et doit faire moins de 5 caractères /// [Required] [MaxLength(5)] public string Surname { get; set; } /// /// L'Age est requis /// [Required] public int? Age { get; set; } /// /// Méthode qui permet de chercher tous les attributs et de valider ceux-ci /// /// /// public string this[string columnName] { get { var validationResults = new List(); if (Validator.TryValidateProperty( GetType().GetProperty(columnName).GetValue(this) , new ValidationContext(this) { MemberName = columnName } , validationResults)) return null; return validationResults.First().ErrorMessage; } } public string Error { get; } } }