TrimExtension.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace gpt_api.Core.Extensions
  8. {
  9. /// <summary>
  10. /// 去除空格扩展方法
  11. /// </summary>
  12. public static class TrimExtension
  13. {
  14. /// <summary>
  15. /// 对象去除去除空格
  16. /// </summary>
  17. /// <param name="obj">对象</param>
  18. /// <returns></returns>
  19. public static object ToTrimString(this object obj)
  20. {
  21. try
  22. {
  23. Type type = obj.GetType();
  24. PropertyInfo[] props = type.GetProperties();
  25. Parallel.ForEach(props, p =>
  26. {
  27. if (p.PropertyType.Name.Equals("String"))
  28. {
  29. var tmp = (string)p.GetValue(obj, null);
  30. if (tmp != null)
  31. {
  32. p.SetValue(obj, tmp.Trim(), null);
  33. }
  34. }
  35. });
  36. return obj;
  37. }
  38. catch
  39. {
  40. return obj;
  41. }
  42. }
  43. }
  44. }