using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace gpt_api.Core.Extensions { /// /// 去除空格扩展方法 /// public static class TrimExtension { /// /// 对象去除去除空格 /// /// 对象 /// public static object ToTrimString(this object obj) { try { Type type = obj.GetType(); PropertyInfo[] props = type.GetProperties(); Parallel.ForEach(props, p => { if (p.PropertyType.Name.Equals("String")) { var tmp = (string)p.GetValue(obj, null); if (tmp != null) { p.SetValue(obj, tmp.Trim(), null); } } }); return obj; } catch { return obj; } } } }