遍历实体类拼接键值字符串

在掉用接口时,有时难免要拼接实体类字段值的字符串。有方法如下:

 1         /// <summary>
 2         /// 获取实体类所有字段取值
 3         /// </summary>
 4         /// <param name="t">实体类</param>
 5         /// <param name="action">Add</param>
 6         /// <returns></returns>
 7         public static string ReturnEntityStr(object t, string action)
 8         {
 9             string tStr = string.Empty;
10             if (t == null)
11             {
12                 return tStr;
13             }
14             System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
15 
16             if (properties.Length <= 0)
17             {
18                 return tStr;
19             }
20             foreach (System.Reflection.PropertyInfo item in properties)
21             {
22                 string name = item.Name;
23                 object value = item.GetValue(t, null);
24                 if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
25                 {
26                     if (value is DateTime)
27                     {
28                         value = Convert.ToDateTime(value).ToString("yyyy-MM-dd hh:mm:ss");
29                     }
30                     if (name != "DataTable_Action_" && value != System.DBNull.Value && value != "" && value != null)
31                     {
32                         if (action == "Add" && name == "id")
33                         {
34                             continue;
35                         }
36                         tStr += string.Format("&{0}={1}", name, value);
37                     }
38                 }
39             }
40             return tStr;
41         }
42 
43         /// <summary>
44         /// 获取部分字段和字段对应的值
45         /// </summary>
46         /// <param name="t">实体类</param>
47         /// <param name="strArr">筛选字段</param>
48         /// <returns></returns>
49         public static string ReturnEntityStr(object t, ArrayList strArr)
50         {
51             string tStr = string.Empty;
52             if (t == null)
53             {
54                 return tStr;
55             }
56             System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
57 
58             if (properties.Length <= 0)
59             {
60                 return tStr;
61             }
62             foreach (System.Reflection.PropertyInfo item in properties)
63             {
64                 string name = item.Name;
65                 object value = item.GetValue(t, null);
66                 if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
67                 {
68                     if (value is DateTime)
69                     {
70                         value = Convert.ToDateTime(value).ToString("yyyy-MM-dd HH:mm:ss");
71                     }
72 
73                     if (((IList)strArr).Contains(name) && value != System.DBNull.Value && value != "" && value != null)
74                     {
75                         tStr += string.Format("&{0}={1}", name, value);
76                     }
77                 }
78             }
79             return tStr;
80         }

 

posted @ 2015-05-26 11:05  ybyi  阅读(669)  评论(0)    收藏  举报