Request参数值自动去空格
/// <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参数值自动去空格的更多相关文章
- 日期,为下拉列表添加日期,优化,目前本人博客上最优的解决方案,之前学习的通过判断得到平年闰年,而这个是让系统自动去判断,无须if判断,代码示例
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"% ...
- WordPress实现中英文数字之间自动加空格排版
通常来说中文与英文.中文和数字之间加上空格的排版会更加好看,但是如果让我们在编辑文章的时候人工添加,感觉非常繁琐和让人厌烦,所以今天龙笑天下就来跟大家介绍一下WordPress如何实现中英文数字之间自 ...
- ORACLE对字符串去空格处理(trim)
首先便是这Trim函数.Trim 函数具有删除任意指定字符的功能,而去除字符串首尾空格则是trim函数被使用频率最高的一种.语法Trim ( string ) ,参数string:string类型,指 ...
- 11月8日下午Jquery取属性值(复选框、下拉列表、单选按钮)、做全选按钮、JSON存储、去空格
1.jquery取复选框的值 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...
- jQuery取复选框值、下拉列表里面的属性值、取单选按钮的属性值、全选按钮、JSON存储、*去空格
1.jquery取复选框的值<!--引入jquery包--> <script src="../jquery-1.11.2.min.js"></scri ...
- EditText中输入手机号码时,自动添加空格
输入手机号码时,自动添加空格,更容易辨别 public class PhoneWatcher implements TextWatcher { private EditText _text; publ ...
- js使用正则表达式去空格
写成类的方法格式如下:(str.trim();) <script language="javascript"> String.prototype.trim=functi ...
- PHP trim去空格函数
trim() 能除去的字符有“ ”空格."\t"水平制表符."\n"换行符."\r"回车符."\0字符串结束符".&qu ...
- Jquery取属性值(复选框、下拉列表、单选按钮)、做全选按钮、JSON存储、去空格
1.jquery取复选框的值 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "htt ...
随机推荐
- npm下载安装文件太慢..修改这个就好了..治好多年的便秘..真香预警
修改 npm 的安装目录下的 npmrc文件 增加一条 registry=http://registry.cnpmjs.org 将原来的https改成下面的http $ npm config set ...
- Openface 入门
Openface 简单入门 背景 Openface是一个开源的人脸识别框架,同类软件产品还有 seetaface ,DeepID等,当然,如果算上商业的产品,那就更多了. Openface人脸比对结果 ...
- SpringBoot 出现Whitelabel Error Page 解决办法
这是咋了,咋的就404了 我路径也挺对的啊 注解也都写上了啊 咋就找不到了呢? debug吧它不进方法 看日志吧,他还不报错 这家伙给我急的 百度一下午也没解决,最后还是看官网才知道错在了那里,程序只 ...
- leetcode543
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...
- PowerDesign生成数据库
最近要忙期考,但还是决定每天抽点空来写CodeSmith的系列文章了,在此实在不敢用教程这个词语,毕竟自己对CodeSmith了解的也不是很多,有很多牛人都在博客园发布了不少关于CodeSmith的文 ...
- 关于吴恩达机器学习支持向量机的问题,讲到对偶前有一个最小化f(w)使用拉格朗日求解时转化成一个最大的相等式的理解和一些困惑
(纯属个人理解) 参考: https://www.zhihu.com/question/267482928 https://www.cnblogs.com/90zeng/p/Lagrange_dual ...
- Handler相关
1.延迟方法 Message msg = new Message(); msg.what = 0x111; // netWorkHandler.sendMessage(msg); //延迟方法三 ...
- [leetcode]236. Lowest Common Ancestor of a Binary Tree二叉树最近公共祖先
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accordi ...
- [leetcode]57. Insert Interval插入区间
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- MySQL 树节点递归遍历所以子节点
DELIMITER $$ DROP FUNCTION IF EXISTS `getChildList`$$ CREATE FUNCTION `getChildList`(rootId INT) RET ...