zoukankan      html  css  js  c++  java
  • 临时写的一个通用必填判断方法

            private static ConcurrentDictionary<string, object> dictCache = new ConcurrentDictionary<string, object>();
    
            #region 得到类里面的属性集合
            /// <summary>
            /// 得到类里面的属性集合
            /// </summary>
            /// <param name="type"></param>
            /// <param name="columns"></param>
            /// <returns></returns>
            public static PropertyInfo[] GetProperties(Type type, string[] columns = null)
            {
                PropertyInfo[] properties = null;
                if (dictCache.ContainsKey(type.FullName))
                {
                    properties = dictCache[type.FullName] as PropertyInfo[];
                }
                else
                {
                    properties = type.GetProperties();
                    dictCache.TryAdd(type.FullName, properties);
                }
    
                if (columns != null && columns.Length > 0)
                {
                    //  按columns顺序返回属性
                    var columnPropertyList = new List<PropertyInfo>();
                    foreach (var column in columns)
                    {
                        var columnProperty = properties.Where(p => p.Name == column).FirstOrDefault();
                        if (columnProperty != null)
                        {
                            columnPropertyList.Add(columnProperty);
                        }
                    }
                    return columnPropertyList.ToArray();
                }
                else
                {
                    return properties;
                }
            }
            #endregion
    
            /// <summary>
            /// 检查是否为空
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="entity"></param>
            /// <param name="columns"></param>
            /// <returns></returns>
            public static string CheckIsMust<T>(T entity,string[] columns)
            {
                string description = string.Empty;
                Type type = typeof(T);
                PropertyInfo[] properties = GetProperties(type, columns);
                for (int columnIndex = 0; columnIndex < properties.Length; columnIndex++) {
                    DescriptionAttribute customAttribute = (DescriptionAttribute)Attribute.GetCustomAttribute(properties[columnIndex], typeof(DescriptionAttribute));
                    if (customAttribute != null)
                    {
                        string drValue = Convert.ToString(properties[columnIndex].GetValue(entity, null));
                        if (string.IsNullOrEmpty(drValue))
                        {
                            description = customAttribute.Description+"不能为空!";                        
                            break;
                        }
                    }
                }
                return description;
            }
    

      使用方式:

            实体代码
            /// <summary>
            /// 入参
            /// </summary>
            public class ShipmentEntity 
            {
                /// <summary>
                /// 出货单号
                /// WDN号
                /// </summary>
                [Description("出货单号")]
                public string shipment_no { get; set; }
                /// <summary>
                /// 货柜号
                /// 国外---集装箱号,国内---车牌号
                /// </summary>
                [Description("货柜号")]
                public string container_no { get; set; }
                /// <summary>
                /// CI号
                /// DN号
                /// </summary>
                [Description("CI号")]
                public string ci_no { get; set; }
                /// <summary>
                /// 运输类型
                /// 0:陆运 1:海运 2:空运
                /// </summary>
                [Description("运输类型")]
                public string shipment_type { get; set; }
                /// <summary>
                /// 出货日期
                /// </summary>
                [Description("出货日期")]
                public DateTime shipment_date { get; set; }
                /// <summary>
                /// 是否是APP
                /// 0:不是 1:是 2:空
                /// </summary>
                [Description("APP")]
                public string ship_goto { get; set; }
                /// <summary>
                /// 客检
                /// 否:0  是:1
                /// </summary>
                public string custcheck { get; set; }
                /// <summary>
                /// 出货操作人
                /// </summary>
                [Description("出货操作人")]
                public string shipment_operator { get; set; }
                /// <summary>
                /// 托盘列表
                /// </summary>
                public List<string> palletnos = new List<string>();
            }
    

      

      

    string description = Common.CheckIsMust<API.Model.Shipment.ShipmentEntity>(commonparameters, new string[] { "shipment_no", "container_no", "ci_no", "shipment_type", "shipment_date", "ship_goto", "shipment_operator" });
                   
                    if (!string.IsNullOrEmpty(description))
                    {
                        commonResult.SetReturnFail(description);
                        result = base.Content(Common.SerializeResultCommon<API.Model.Shipment.ShipmentReturn>(null, commonResult), "application/json");
                        return result;
                    }
    

      一般都是使用model验证方式,暂时只有简单的两个接口,就不整体调整了。

  • 相关阅读:
    Activity生命周期 onCreate onResume onStop onPause (转)
    职场链接
    memset中的sizeof
    c文件操作 (转)
    串口编程 tcflush()函数 (转)
    tcflush 功能(转)
    串口开发(转)
    ios github网址
    做移动端视频通话软件,大致看了下现有的开源软件(转)
    PJSIP框架
  • 原文地址:https://www.cnblogs.com/sailing92/p/14028761.html
Copyright © 2011-2022 走看看