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

  • 相关阅读:
    python递归函数
    python全局替换文件内容脚本第1版
    python的if判断补充
    python装饰器
    python函数作用域
    python函数基础
    python文件操作
    ASCII、Unicode和UTF-8编码的区别
    python基础(二)----数据类型
    python基础第一章
  • 原文地址:https://www.cnblogs.com/chengleijiang/p/5693861.html
Copyright © 2020-2023  润新知