很好用的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 ...
随机推荐
- Python之路第一课Day3--随堂笔记(文件操作)
一.集合的介绍 1.集合操作 集合是一个无序的,不重复的数据组合,它的主要作用如下: 去重,把一个列表变成集合,就自动去重了 关系测试,测试两组数据之前的交集.差集.并集等关系 常用操作 s = se ...
- jquery插件学习之元素顶部悬浮
jquery插件的学习: HTML部分及应用 <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- jQueryMobile引入文件后样式无法正常显示
jQueryMobile引入文件后样式无法正常显示解决方法: jQuery文件必须放在jQueryMobile文件之前 eg:
- 给备战NOIP 2014 的战友们的10条建议
应老胡要求,要写10条建议= = begin 1. 注意文件关联 比如 halt 前要close(input); close(output); 还有就是一定要打这两句话= = 2. 快排,大家都懂得. ...
- javaScript中其他类型的值转换为Boolean类型
将javaScript中其他任意类型的值转换为对应Boolean类型的值. 一 将number类型的值转换为Boolean类型 数值为0: var myBoolean = new Boolean(0 ...
- win 10 常见问题
Microsoft Edge Can't Open Local Domains IE Edge浏览器打不开本地站点 解决方法:解决方法
- MySQL运行状态show status详解
状态名 作用域 详细解释 Aborted_clients Global 由于客户端没有正确关闭连接导致客户端终止而中断的连接数 Aborted_connects Global 试图连接到MySQL服务 ...
- 一起来做chrome扩展《基础介绍》
首先说明,chrome的扩展并不它的插件,网上很多说写插件,其实都是说的扩展.写扩展并不复杂,只要根据chrome提供的一系列的API进行就可以实现很多的功能.只是对API的学习是有代价的,加上国内访 ...
- js自定义弹出框
js自定义弹出框: 代码如下 <html> <head><title>自定义弹出对话框</title> <style type ="te ...
- js null 和 undefined
undefined是一个特殊类型,null本质上是一个对象 typeof undefined//"undefined"typeof null//"object" ...