1. /// <summary>
  2. /// TypeTrimHelper
  3. /// </summary>
  4. public static class TypeTrimHelper
  5. {
  6. /// <summary>
  7. /// 类型字典
  8. /// </summary>
  9. public static ConcurrentDictionary<Type, PropertyInfo[]> TypeDictionary
  10. = new ConcurrentDictionary<Type, PropertyInfo[]>();
  11. /// <summary>
  12. /// 获取Type属性
  13. /// </summary>
  14. /// <param name="type"></param>
  15. /// <returns></returns>
  16. public static PropertyInfo[] GetTypeProperties(Type type)
  17. {
  18. PropertyInfo[] typeProperties = null;
  19. if (TypeDictionary.ContainsKey(type))
  20. {
  21. typeProperties = TypeDictionary[type];
  22. }
  23. else
  24. {
  25. typeProperties = type.GetProperties();
  26. TypeDictionary[type] = typeProperties;
  27. }
  28. return typeProperties;
  29. }
  30. private static bool HasNotAutoTrimSpacesAttribute(Type type)
  31. {
  32. return type.GetCustomAttribute<NotAutoTrimSpacesAttribute>()
  33. != null;
  34. }
  35. private static bool HasNotAutoTrimSpacesAttribute(PropertyInfo propertyInfo)
  36. {
  37. return propertyInfo.GetCustomAttribute<NotAutoTrimSpacesAttribute>()
  38. != null;
  39. }
  40. /// <summary>
  41. /// 去空格操作
  42. /// </summary>
  43. /// <param name="objType">类型</param>
  44. /// <param name="obj">当前对象</param>
  45. public static void TypeTrim(Type objType, object obj)
  46. {
  47. if (HasNotAutoTrimSpacesAttribute(objType) || obj == null) { return; }
  48. PropertyInfo[] typeProperties = GetTypeProperties(objType);
  49. foreach (var typeProperty in typeProperties)
  50. {
  51. if (HasNotAutoTrimSpacesAttribute(typeProperty)) { continue; }
  52. var cPropertyType = typeProperty.PropertyType;
  53. if (cPropertyType == typeof (string))
  54. {
  55. if (!typeProperty.CanWrite)
  56. {
  57. continue;
  58. }
  59. string value = typeProperty.GetValue(obj) as string;
  60. if (value != null)
  61. {
  62. typeProperty.SetValue(obj, value.Trim());
  63. }
  64. }
  65. else
  66. {
  67. if (cPropertyType.IsClass)
  68. {
  69. if (cPropertyType.IsValueType)
  70. {
  71. continue;
  72. }
  73. if (cPropertyType.GetInterface(typeof (IEnumerable).Name, false) != null)
  74. {
  75. var values = typeProperty.GetValue(obj) as IEnumerable;
  76. if (values != null)
  77. {
  78. var enumerator = values.GetEnumerator();
  79. while (enumerator.MoveNext())
  80. {
  81. if (enumerator.Current != null)
  82. {
  83. var itemType = enumerator.Current.GetType();
  84. TypeTrim(itemType, enumerator.Current);
  85. }
  86. }
  87. }
  88. }
  89. else
  90. {
  91. TypeTrim(cPropertyType, typeProperty.GetValue(obj));
  92. }
  93. }
  94. }
  95. }
  96. }
  97. }

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. 插件: Hammer.js

    官网: http://hammerjs.github.io/  hammer.js 官网 http://hammerjs.github.io/api/ 官网API(官网写的实在太简了!不好用.注意里面 ...

  2. java利用泛型实现不同类型可变参数

    public class VP { public <T> void printMsg(T... args){ for (T t:args){ System.out.println(&quo ...

  3. linux文件属性的10个字符各代表什么意思

    10个字符表示文件类别和权限,具体如下: 例: 第一个字符表示文件类别,代表的含义如下:-:普通文件d:目录文件b:块设备文件c:字符设备文件l:符号链接文件 后面9个字符代表3组访问权限:第1组的3 ...

  4. Mybatis运行错误:信息: SQLErrorCodes loaded: [DB2, Derby, H2, HDB, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]

    Mybatis运行出现错误提示: 五月 23, 2018 12:07:22 上午 org.springframework.jdbc.support.SQLErrorCodesFactory <i ...

  5. BUILDING WITH BOOTSTRAP

    BUILDING WITH BOOTSTRAP Bootstrap Generalizations You just built an impressive webpage using the Boo ...

  6. IDEA使用SpringBoot 、maven创建微服务的简单过程

    使用IDEA新建一个简单的微服务 1. 打开IDEA,File -> New  -> project 打开如下图1-1所示的对话框 图 1-1 2.点击"Next"按钮 ...

  7. 权限控制框架---shiro入门

    1.shiro控制权限的两种思想:粗粒度url级别,细粒度方法级别 2.shiro执行流程简介 3.案例 3.1shiro控制用户登录实现,登录其实就是shiro中的认证. (1)配置web.xml( ...

  8. php 计算 距离

    function getdistance($lng1,$lat1,$lng2,$lat2){ //将角度转为狐度 $radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为 ...

  9. xml文件中&符号需要转义为&amp;

    xml文件中&符号需要转义为&

  10. vw, vh视区覆盖和自适应图片

      CSS .wrap{width:100vw;height:100vh;background: rgba(0,0,0,0.3);position: fixed;top:0;left:0;text-a ...