/// <summary>
/// TypeTrimHelper
/// </summary>
public static class TypeTrimHelper
{
/// <summary>
/// 类型字典
/// </summary>
public static ConcurrentDictionary<Type, PropertyInfo[]> TypeDictionary
= new ConcurrentDictionary<Type, PropertyInfo[]>(); /// <summary>
/// 获取Type属性
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
public static PropertyInfo[] GetTypeProperties(Type type)
{
PropertyInfo[] typeProperties = null;
if (TypeDictionary.ContainsKey(type))
{
typeProperties = TypeDictionary[type];
}
else
{
typeProperties = type.GetProperties();
TypeDictionary[type] = typeProperties;
} return typeProperties;
} private static bool HasNotAutoTrimSpacesAttribute(Type type)
{
return type.GetCustomAttribute<NotAutoTrimSpacesAttribute>()
!= null;
} private static bool HasNotAutoTrimSpacesAttribute(PropertyInfo propertyInfo)
{
return propertyInfo.GetCustomAttribute<NotAutoTrimSpacesAttribute>()
!= null;
} /// <summary>
/// 去空格操作
/// </summary>
/// <param name="objType">类型</param>
/// <param name="obj">当前对象</param>
public static void TypeTrim(Type objType, object obj)
{
if (HasNotAutoTrimSpacesAttribute(objType) || obj == null) { return; } PropertyInfo[] typeProperties = GetTypeProperties(objType);
foreach (var typeProperty in typeProperties)
{
if (HasNotAutoTrimSpacesAttribute(typeProperty)) { continue; } var cPropertyType = typeProperty.PropertyType;
if (cPropertyType == typeof (string))
{
if (!typeProperty.CanWrite)
{
continue;
} string value = typeProperty.GetValue(obj) as string;
if (value != null)
{
typeProperty.SetValue(obj, value.Trim());
}
}
else
{
if (cPropertyType.IsClass)
{
if (cPropertyType.IsValueType)
{
continue;
} if (cPropertyType.GetInterface(typeof (IEnumerable).Name, false) != null)
{
var values = typeProperty.GetValue(obj) as IEnumerable;
if (values != null)
{
var enumerator = values.GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Current != null)
{
var itemType = enumerator.Current.GetType();
TypeTrim(itemType, enumerator.Current);
}
}
}
}
else
{
TypeTrim(cPropertyType, typeProperty.GetValue(obj));
}
}
}
}
} }

Request参数值自动去空格的更多相关文章

  1. 日期,为下拉列表添加日期,优化,目前本人博客上最优的解决方案,之前学习的通过判断得到平年闰年,而这个是让系统自动去判断,无须if判断,代码示例

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...

  2. WordPress实现中英文数字之间自动加空格排版

    通常来说中文与英文.中文和数字之间加上空格的排版会更加好看,但是如果让我们在编辑文章的时候人工添加,感觉非常繁琐和让人厌烦,所以今天龙笑天下就来跟大家介绍一下WordPress如何实现中英文数字之间自 ...

  3. ORACLE对字符串去空格处理(trim)

    首先便是这Trim函数.Trim 函数具有删除任意指定字符的功能,而去除字符串首尾空格则是trim函数被使用频率最高的一种.语法Trim ( string ) ,参数string:string类型,指 ...

  4. 11月8日下午Jquery取属性值(复选框、下拉列表、单选按钮)、做全选按钮、JSON存储、去空格

    1.jquery取复选框的值 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

  5. jQuery取复选框值、下拉列表里面的属性值、取单选按钮的属性值、全选按钮、JSON存储、*去空格

    1.jquery取复选框的值<!--引入jquery包--> <script src="../jquery-1.11.2.min.js"></scri ...

  6. EditText中输入手机号码时,自动添加空格

    输入手机号码时,自动添加空格,更容易辨别 public class PhoneWatcher implements TextWatcher { private EditText _text; publ ...

  7. js使用正则表达式去空格

    写成类的方法格式如下:(str.trim();) <script language="javascript"> String.prototype.trim=functi ...

  8. PHP trim去空格函数

    trim() 能除去的字符有“ ”空格."\t"水平制表符."\n"换行符."\r"回车符."\0字符串结束符".&qu ...

  9. Jquery取属性值(复选框、下拉列表、单选按钮)、做全选按钮、JSON存储、去空格

    1.jquery取复选框的值 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...

随机推荐

  1. leetcode56

    public class Solution { public IList<Interval> Merge(IList<Interval> intervals) { var le ...

  2. BASEDIR

    1)正常写python程序会有一个可执行的bin.py文件,假如这个文件需要导入my_module里面定义的模块,应该怎么设置sys.path(此时可以直接导入), 因为bin和model属于同级目录 ...

  3. setTimeout应用例子-移入移出div显示和隐藏

    效果:移入div1,div2保持显示,移出div1,div2消失. 移入div2,div2保持显示,移出div2,div2消失. 一.HTML代码 <div id='div1'></ ...

  4. Sql Server数据库之identity(自增)

    一.identity的基本用法 1.identity的含义: identity表示该字段的值会自动更新,通常情况下,不允许直接修改identity修饰的字段,否则编译会报错 2.基本语法 列名  数据 ...

  5. SQL Server 磁盘请求超时的833错误原因分析以及解决

    本文出处:http://www.cnblogs.com/wy123/p/6984885.html 最近遇到一个SQL Server服务器响应极度缓慢,并且出现客户端请求报错的情况,在数据库中的erro ...

  6. 目标检测之faster-RCNN和FPN

    今年(2017年第一季度),何凯明大神出了一篇文章,叫做fpn,全称是:feature pyramid network for object Detection,为什么发这篇文章,根据 我现在了解到的 ...

  7. jquery抓娃娃机代码

    <html><head><title>jQuery抓娃娃机游戏代码 - 源码之家</title><meta name="content- ...

  8. c++ 面试题(网络类)

    1,若服务器方单独终止之后,客户端继续发数据会怎么样? https://blog.csdn.net/Nick_666/article/details/78342442 https://www.zhih ...

  9. SpringBoot集成Jasypt安全框架,配置文件内容加密

    我们在SpringBoot项目中的yml或者properties配置文件中都是明文的,相对而言安全性就低了很多.都知道配置文件中的都是一些数据库连接用户名密码啊.一些第三方密钥等信息.所以我们谨慎点, ...

  10. Java15-java语法基础(十四)抽象类

    Java15-java语法基础(十四)抽象类 一.抽象类的作用 三个类都有"执行任务"的方法,分别在这三个类中进行定义,因此需要重复编写代码,降低了程序开发效率,且增加了程序出现错 ...