有时会遇到很多不合理的数据附件到实体后有大量空格需要处理,这里提供一个方法,通过并行反射的方式高效清理空格。

 

Code:

  1. //清除字符串空格
  2. public static object TrimString(object obj)
  3. {
  4.     try
  5.     {
  6.         Type t = obj.GetType();
  7.         PropertyInfo[] props = t.GetProperties();
  8.  
  9.         Parallel.ForEach(props, p =>
  10.         {
  11.             if (p.PropertyType.Name == "String")
  12.             {
  13.                 var tmp = (string)p.GetValue(obj, null);
  14.                 p.SetValue(obj, tmp.Trim(), null);
  15.             }
  16.         });
  17.  
  18.         return obj;
  19.     }
  20.     catch
  21.     {
  22.         return obj;
  23.     }
  24. }

相关文章:

  • 2021-07-08
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2021-08-19
  • 2021-09-25
  • 2022-12-23
  • 2022-02-13
猜你喜欢
  • 2021-08-22
  • 2021-11-07
  • 2022-12-23
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案