记忆一下,以防遗忘
定义一个接口
1 public interface IPropertyValidation 2 { 3 /// <summary> 4 /// 验证失败后的错误信息 5 /// </summary> 6 string ErrorInfo { get; set; } 7 8 /// <summary> 9 /// 是否通过验证 默认为空 10 /// </summary> 11 bool? IsValidation { get; set; } 12 13 /// <summary> 14 /// 验证单个属性 15 /// </summary> 16 /// <param name="obj"></param> 17 /// <param name="propertyName"></param> 18 void Validate(object obj, string propertyName); 19 20 ///// <summary> 21 ///// 验证所有属性 22 ///// </summary> 23 ///// <param name="obj"></param> 24 //void ValidatAll(object obj); 25 }
实现两个特性类
1 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] 2 public class StringLengthAttribute : Attribute, IPropertyValidation 3 { 4 private int _max; 5 private int _min; 6 public string ErrorInfo { get; set; } 7 public bool? IsValidation { get; set; } 8 9 public StringLengthAttribute(int min, int max) 10 { 11 this._min = min; 12 this._max = max; 13 } 14 15 public void Validate(object obj, string propertyName) 16 { 17 var type = obj.GetType(); 18 var property = type.GetProperty(propertyName);//获取特定属性; 19 if (property.IsDefined(typeof(IPropertyValidation), true))//如果属性定义了这个特性 20 { 21 var value = (string)property.GetValue(obj);//获取属性值 22 if (value.Length < this._min || value.Length > this._max) 23 { 24 this.IsValidation = false; 25 } 26 else 27 { 28 this.IsValidation = true; 29 } 30 } 31 } 32 } 33 34 35 36 [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] 37 internal class PhoneNumberAttribute : Attribute, IPropertyValidation 38 { 39 public string ErrorInfo { get; set; } 40 public bool? IsValidation { get; set; } 41 42 public void Validate(object obj, string propertyName) 43 { 44 var type = obj.GetType(); 45 var property = type.GetProperty(propertyName); 46 if (property.IsDefined(typeof(IPropertyValidation), true)) 47 { 48 var value = (string)property.GetValue(obj);//获取属性值 49 if (value.Length != 11) 50 { 51 this.IsValidation = false; 52 } 53 else 54 { 55 this.IsValidation = true; 56 } 57 } 58 } 59 }
实现一个方法获取验证信息
1 public static void PropetyAtrributeValidation(object obj) 2 { 3 var type = obj.GetType(); 4 var properties = type.GetProperties(); 5 foreach (var property in properties) 6 { 7 if (property.IsDefined(typeof(IPropertyValidation), true)) 8 { 9 var attribute = (IPropertyValidation)property.GetCustomAttributes(typeof(IPropertyValidation), true)[0]; 10 attribute.Validate(obj, property.Name);//验证属性值 11 if (attribute.IsValidation.HasValue && !attribute.IsValidation.Value) 12 { 13 Console.WriteLine($"{attribute.ErrorInfo}"); 14 } 15 } 16 } 17 }
定义一个模型类
1 public class Test 2 { 3 public int Age { get; set; } 4 5 [StringLength(5, 10, ErrorInfo = "名称长度需要大于5且小于10")] 6 public string Name { get; set; } 7 8 [StringLength(2, 4, ErrorInfo = "名称长度需要大于2且小于4")] 9 public string NickName { get; set; } 10 11 [PhoneNumber(ErrorInfo = "电话号码长度需要11位")] 12 public string PhoneNumber { get; set; } 13 }
运行
1 private static void Main(string[] args) 2 { 3 var test = new Test() 4 { 5 Name = "ABCDEFG",//符合规则 6 NickName = "A",//不符合规则 7 PhoneNumber = "185123456789",//不符合规则 8 }; 9 PropetyAtrributeValidation(test); 10 }
结果