1. 程式人生 > 其它 >C#特性的一些用法(2)

C#特性的一些用法(2)

記憶一下,以防遺忘

定義一個介面

 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         }

結果