我设计的数据库的一些字符串字段是固长字符串 从里面取出实体的值后面有多余的空格
于是我就想把这些多余的空格去掉
写了一个去除实体中字符串属性多余空格的方法 用到了反射
这样就能把多余的空格去除了
-
-
-
-
-
- public static object TrimStr(object obj)
- {
- Type t = obj.GetType();
-
- PropertyInfo[] properties = t.GetProperties();
-
-
- foreach (PropertyInfo p in properties)
- {
- if (p.PropertyType.Name == "String")
- {
-
- string str = (string)p.GetValue(obj, null);
-
- p.SetValue(obj, str.Trim(), null);
- }
- }
-
- return obj;
- }