1. 首页>
  2. 技术文章>
  3. netcore对实体中所有的字符串去掉首尾空格

netcore对实体中所有的字符串去掉首尾空格

2/24/23 11:18:21 AM 浏览 1343 评论 0

netcore trim

/// <summary>
/// 实体中的字符串去掉前后空格
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
public static void Trim<T>(this T model, BindingFlags bindingAttr = BindingFlags.Public | BindingFlags.Instance) where T : class
{
    if (model is null) { return; }
    var stringProperties = typeof(T).GetProperties(bindingAttr).Where(p => p.PropertyType == typeof(string));
    foreach (var strProp in stringProperties)
    {
        string value = strProp.GetValue(model)?.ToString().Trim() ?? string.Empty;
        strProp.SetValue(model, value);
    }
}


网友讨论