• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
菲儿——看时间流逝,风不止
博客园    首页    新随笔    联系   管理    订阅  订阅

很好用的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;
}
}
}

posted @ 2016-07-22 08:57  时间都疯了,你知道吗?  阅读(1179)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3