输出对象的字段名和值
在单元测试时,通过反射获取对象的属性值。
ToString
1 private static string OutMessage(object lab)
2 {
3 Type t = lab.GetType();
4 StringBuilder sb = new StringBuilder();
5 foreach (var p in t.GetProperties())
6 {
7 if (p.DeclaringType.Namespace == t.Namespace)
8 {
9 string value = p.GetValue(lab, null).ToString();
10 if (string.IsNullOrEmpty(value))
11 continue;
12
13 if (sb.Length > 0)
14 sb.Append(",");
15 sb.AppendFormat("{0}={1}", p.Name, p.GetValue(lab, null));
16 }
17 }
18 return sb.ToString();
19 }
2 {
3 Type t = lab.GetType();
4 StringBuilder sb = new StringBuilder();
5 foreach (var p in t.GetProperties())
6 {
7 if (p.DeclaringType.Namespace == t.Namespace)
8 {
9 string value = p.GetValue(lab, null).ToString();
10 if (string.IsNullOrEmpty(value))
11 continue;
12
13 if (sb.Length > 0)
14 sb.Append(",");
15 sb.AppendFormat("{0}={1}", p.Name, p.GetValue(lab, null));
16 }
17 }
18 return sb.ToString();
19 }