反射 获取实体值并验证

最近项目中涉及到验证入参不能为null的问题,不想每个方法都写一套验证方法,遂想写一个公用方法,自然就想到了反射,

由于入参类型未知,不过大致有这么几种类型:数组,类,泛型还有简单的字符串,所以这里我用了"T",由于有可能

有数组或者泛型,那么自然就想到了 递归

首先我们需要定义一个实体类型,尽量包含上述的几种类型,ParameterDescription是我们自定义的特性,只有IsRequired为true的我们才进行判断:

 

 1         public static string JudgeNull<T>(T obj)
 2         {
 3             string tag = "true";
 4             if (obj != null)
 5             {
 6                 Type t = obj.GetType();
 7                 // 如果是泛型 则 使用 IEnumerable 枚举,然后循环 递归判断
 8                 if (t.IsGenericType)
 9                 {
10                     IEnumerable<T> Generic = (IEnumerable<T>)obj;
11                     foreach (T a in Generic)
12                     {
13                         tag = JudgeNull(a);
14                         if (tag != "true")
15                         {
16                             return tag;
17                         }
18                     }
19                 }
20                 else
21                 {
22                     PropertyInfo[] props = t.GetProperties();
23                     foreach (var item in props)
24                     {
25                         var pValue = t.GetProperty(item.Name);
26                         //
27                         ParameterDescription attrObj = (ParameterDescription)item.GetCustomAttribute(typeof(ParameterDescription));
28                         if (attrObj == null || attrObj.IsRequired == true)
29                         {
30                             #region 条件判断
31                            // 这里判断是否是类或者数组
32                             if (pValue.PropertyType.IsArray ||
33                                           (pValue.PropertyType.IsClass &&
34                                           !pValue.PropertyType.IsGenericType &&
35                                           !pValue.PropertyType.Equals(typeof(String))))
36 
37                             {
38                                 var cla = item.GetValue(obj);
39                                 // 对象值为空 直接return
40                                 if (cla == null)
41                                 {
42                                     return item.Name + "不能为null!";
43                                 }
44                                 // 递归
45                                 tag = JudgeNull(cla);
46                                 if (tag != "true")
47                                 {
48                                     return tag;
49                                 }
50                             }
51                             // 这里判断是否是泛型
52                             else if (pValue.PropertyType.IsGenericType)//
53                             {
54                                 var list = item.GetValue(obj);
55                                 // 对象值为空 直接return
56                                 if (list == null)
57                                 {
58                                     return item.Name + "不能为null!";
59                                 }
60                                 tag = JudgeNull(list);
61                                 if (tag != "true")
62                                 {
63                                     return tag;
64                                 }
65                             }
66                             // 简单类型
67                             else
68                             {
69                                 var o = item.GetValue(obj);
70                                 if (o == null)
71                                 {
72                                     return item.Name + "不能为null!";
73                                 }
74                                 else if (string.IsNullOrWhiteSpace(o.ToString()))
75                                 {
76                                     return item.Name + "不能为空或者空字符串!";
77                                 }
78                             }
79                             #endregion
80                         }
81 
82                     }
83                 }
84             }
85             return tag;
86         }
87     }
View Code

 

然后判断方法如下:

 1         public static string JudgeNull<T>(T obj)
 2         {
 3             string tag = "true";
 4             if (obj != null)
 5             {
 6                 Type t = obj.GetType();
 7                 // 如果是泛型 则 使用 IEnumerable 枚举,然后循环 递归判断
 8                 if (t.IsGenericType)
 9                 {
10                     IEnumerable<T> Generic = (IEnumerable<T>)obj;
11                     foreach (T a in Generic)
12                     {
13                         tag = JudgeNull(a);
14                         if (tag != "true")
15                         {
16                             return tag;
17                         }
18                     }
19                 }
20                 else
21                 {
22                     PropertyInfo[] props = t.GetProperties();
23                     foreach (var item in props)
24                     {
25                         var pValue = t.GetProperty(item.Name);
26                         //
27                         ParameterDescription attrObj = (ParameterDescription)item.GetCustomAttribute(typeof(ParameterDescription));
28                         if (attrObj == null || attrObj.IsRequired == true)
29                         {
30                             #region 条件判断
31                            // 这里判断是否是类或者数组
32                             if (pValue.PropertyType.IsArray ||
33                                           (pValue.PropertyType.IsClass &&
34                                           !pValue.PropertyType.IsGenericType &&
35                                           !pValue.PropertyType.Equals(typeof(String)) &&
36                                           !pValue.PropertyType.IsValueType))
37 
38                             {
39                                 var cla = item.GetValue(obj);
40                                 // 对象值为空 直接return
41                                 if (cla == null)
42                                 {
43                                     return item.Name + "不能为null!";
44                                 }
45                                 // 递归
46                                 tag = JudgeNull(cla);
47                                 if (tag != "true")
48                                 {
49                                     return tag;
50                                 }
51                             }
52                             // 这里判断是否是泛型
53                             else if (pValue.PropertyType.IsGenericType)//
54                             {
55                                 var list = item.GetValue(obj);
56                                 // 对象值为空 直接return
57                                 if (list == null)
58                                 {
59                                     return item.Name + "不能为null!";
60                                 }
61                                 tag = JudgeNull(list);
62                                 if (tag != "true")
63                                 {
64                                     return tag;
65                                 }
66                             }
67                             // 简单类型
68                             else
69                             {
70                                 var o = item.GetValue(obj);
71                                 if (o == null)
72                                 {
73                                     return item.Name + "不能为null!";
74                                 }
75                                 else if (string.IsNullOrWhiteSpace(o.ToString()))
76                                 {
77                                     return item.Name + "不能为空或者空字符串!";
78                                 }
79                             }
80                             #endregion
81                         }
82 
83                     }
84                 }
85             }
86             return tag;
87         }
88     }
View Code

这样就能循环递归入参的任意一个字段,如果有任意一个值为null的话,就会return了。

 

posted @ 2016-06-08 10:19  湛蓝色的回忆  阅读(225)  评论(4)    收藏  举报