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 ...
随机推荐
- ubuntu搭建php开发环境记录
这两天自己在阿里云上面买了一个ecs,系统选的是ubuntu16.04,第一件事就是先搭环境,这次准备使用lamp组合. Apache安装 首先安装apache服务器,ubuntu下面使用apt-ge ...
- activemq 无法消费! consumers are alive when the messages are stuck !
我的微服务中, activemq 消费 一条消息的时候, 出了错, 结果导致了 那条消息就一直处于pending 状态, queue.user.545c2ed5-fee7-482a-bb59-564b ...
- 31_NavLink组件包装优化
简单理解为自定义一个组件并自带样式 import React, {Component} from 'react' import {Switch, Route, Redirect} from 'reac ...
- Linux 网络配置文件解析
Linux 网络配置文件解析 网络配置文件路径/etc/sysconfig/network-scripts/ifcfg-* *代表网卡名 vim /etc/sysconfig/network- ...
- 516. Longest Palindromic Subsequence最长的不连续回文串的长度
[抄题]: Given a string s, find the longest palindromic subsequence's length in s. You may assume that ...
- [leetcode]5. Longest Palindromic Substring最长回文子串
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- freemaker超详细 讲解 配置
一.FreeMarker简介 二.第一个FreeMark示例 2.1.新建一个Maven项目 2.2.添加依赖 2.3.添加存放模板的文件夹 2.4.添加模板 2.5.解析模板 2.6.运行结果 三. ...
- centos 安装解压工作
解压工具: yum install ark 编辑器: yum install gedit
- java 小心使用float和double他可能不如你所想
public static void main(String[] args) { double funds=1.00; ; // ; ;funds>=price;price+=.){ funds ...
- java多线程系列11 juc包下的队列
队列分为两类 阻塞队列 BlockingQueue提供如下两个支持阻塞的方法: (1)put(E e): 尝试把e元素放如BlockingQueue中,如果该队列的元素已满,则阻塞该线程. ...