很好用的request转换为实体方法还有判断实体所有参数不能为空的方法
/// <summary>
/// 模型辅助处理类
/// 2016-04-18
/// </summary>
public class ModelHelper
{
/// <summary>
/// 将数据转化为模型
/// 2016-04-18 基础数据类型:Int32,decimal
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
public static void ConvertToModel<T>(ref T t)
{
System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
Type[] tempArr = new Type[] {typeof(Int32),typeof(decimal)};
object tempObject = null;
for (Int32 i = 0; i < properties.Length; i++)
{
if (ReqUtils.FormString(properties[i].Name) != null || properties[i].PropertyType.Name.ToLower() == "httpfilecollection")
{
switch (properties[i].PropertyType.Name.ToLower())
{
case "int32": tempObject = Int32.Parse(string.Format("0{0}", ReqUtils.FormString(properties[i].Name))); break;
case "decimal": tempObject = decimal.Parse(string.Format("0{0}", ReqUtils.FormString(properties[i].Name))); break;
case "httpfilecollection": tempObject = System.Web.HttpContext.Current.Request.Files; break;
default:
tempObject = string.Format("{0}", ReqUtils.FormString(properties[i].Name));
break;
}
properties[i].SetValue(t, tempObject, null);
}
}
}
/// <summary>
/// 该实体是否都为空
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool ModelIsNotNull(object model)
{
bool istrue = true;
Type type = model.GetType();
var flags = GetOnlyProperty();
var list = type.GetProperties(flags).ToList();
foreach (var p in list)
{
if (IsColumn(p))
{
object value = p.GetValue(model, null) ?? "";
if (string.IsNullOrWhiteSpace(value.ToString()))
{
istrue = false;
break;
}
}
}
return istrue;
}
private BindingFlags GetOnlyProperty()
{
return BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance;
}
private bool IsColumn(PropertyInfo info)
{
object[] objs = info.GetCustomAttributes(typeof(Attribute), true);
return objs.Length == 0;
}
/// <summary>
/// 获取操作提示翻译
/// </summary>
/// <param name="key"></param>
/// <param name="lan"></param>
/// <returns></returns>
public static string GetTans(string key, string lan = "")
{
try
{
if (string.IsNullOrWhiteSpace(lan))
{
lan = "cn";
}
if (lan.ToLower().Contains("cn") || lan.ToLower().Contains("zh"))
{
lan = "zh";
}
string className = "PageInfo_" + lan;
var globalResourceObject = HttpContext.GetGlobalResourceObject(className, key);
if (globalResourceObject != null)
return globalResourceObject.ToString() == "" ? key : globalResourceObject.ToString();
else
{
return key;
}
}
catch
{
return key;
}
}
}
很好用的request转换为实体方法还有判断实体所有参数不能为空的方法的更多相关文章
- Shell脚本中判断输入变量或者参数是否为空的方法
shell判断一个变量是否为空方法总结 https://www.jb51.net/article/154835.htm 1.判断变量 复制代码代码如下: read -p "input a w ...
- Java判断一个字符串str不为空:方法及时间效率
判断一个字符串str不为空的方法有: 1.str == null; 2.”“.equals(str): 3.str.length <= 0; 4.str.isEmpty(): 注意:length ...
- php -- 魔术方法 之 判断属性是否存在或为空:__isset()
属性重载:当访问一个不存在或者权限不够的属性的时候,能够触发一系列的魔术方法,就叫做属性重载 __isset($name):当使用 isset()函数或者empty()函数 判断属性是否存在或者是否为 ...
- Asp.net webapi 判断请求参数是否为空简易方法 Model Validation 判断请求参数是否为空
通常情况下,对于那些经常为别人提供数据接口的开发人员来说,对于调用方传递过来的参数都会有验证处理.例如: if (string.IsNullOrEmpty(entity.Name)) { //当姓名为 ...
- 判断String类型字符串是否为空的方法
在项目中经常遇到要判断String类型的字段是否为空操作 我们可以用Apache提供的StringUtils这个工具类,不用自己去判断,也不用自己封装判断空的方法 它有两个版本,一个是org.apac ...
- 判断一个字符串str不为空的方法
1.str == null; 2."".equals(str); 3.str.length 4.str.isEmpty(); 注意:length是属性,一般集合类对象拥有的属性,取 ...
- 自己设计一个日期类,可以输入年月日作为构造时的参数,如果不使用参数,则设定为1900年1月1日;编写一个方法equals判断两个日期是否相等;另一个方法compareTo可以进行日期之间的比较,返回两个日期之间相差的天数.
import java.util.*; import java.lang.Math; class Date1{ private int year; private int month; private ...
- 通过继承Rect类编写一个具有确定位置的矩形类PlainRect,其确定位置用 矩形的左上角坐标来标识,包含: 添加两个属性:矩形左上角坐标startX和startY。 两个构造方法: 带4个参数的构造方法,用于对startX、startY、width和height属性 初始化; 不带参数的构造方法,将矩形初始化为左上角坐标、长和宽都为0 的矩形; 添加一个方法: 判断某个点是否在矩形内部的方法
package b; public class Rect { Double width; Double height; public Double getWidth() { return width; ...
- Mybatis 传入多个参数查询数据 (3种方法)
第一种方案 DAO层的函数方法 public User selectUser(String name,String area); 对应的Mapper.xml <select id="s ...
随机推荐
- Xamarin的不归路-生成安卓错误
编译生成安卓时提示错误 解决方案:删掉此文件夹(C:\Users\***\AppData\Local\Xamarin\)内所以文件夹和文件,再FQ重新编译即可. 2016年9月1日 13:33
- Python爬虫:Xpath语法笔记
一.选取节点 常用的路劲表达式: 表达式 描述 实例 nodename 选取nodename节点的所有子节点 xpath(‘//div’) 选取了div节点的所有子节点 / 从根节点选取 xpat ...
- IOS网络第三天 - 01-网络文件下载(0922略)
01 数据的安全01 - 密码加密 02 数据的安全02 - 加密过程 01 -数据的安全01 - 本地存储和代码安全 04-网络状态监控 05-真机演示 06-小文件下载 07-大文件下载01-基本 ...
- C fread
fread是一个函数.从一个文件流中读数据,最多读取count个元素,每个元素size字节,如果调用成功返回实际读取到的元素个数,如果不成功返回 0. 函数原型 size_t fread ( void ...
- Mysql创建用户并授权
运行命令行 mysql -uroot -p 登录mysql use mysql; 创建用户:create user 'test123'@'localhost' identified by '12345 ...
- Symmetric Multiprocessor Organization
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION
- Mvc自定义路由让支持.html的格式
前言 在大多时候,我们都需要自定义路由,当我们设置为/list/1.html的时候,有的时候会出现以下异常. routes.MapRoute( "category", // 路由名 ...
- 在mvc中将session的值绑定在页面上
第一步,在SqlServer数据库中创建存储过程,查询的是用户名(员工姓名)所扮演的角色: if exists(select * from sys.objects where name='proc_s ...
- Ueditor百度网页编辑器开发者版java utf-8的使用
index.jsp主要代码: <html> <head> <title>网页编辑器</title> <script type="text ...
- ubuntu 上安装字体
http://www.360doc.com/content/11/0901/23/4171006_145128703.shtml http://www.linuxidc.com/Linux/2008- ...