Exemplo n.º 1
0
        /// <summary>
        ///     检测实体类值状况
        /// </summary>
        /// <param name="dicError">返回错误消息,key:属性名称;vakue:错误消息</param>
        /// <param name="info">要检测的实体</param>
        public static bool Check <TInfo>(this TInfo info, out Dictionary <string, List <string> > dicError) where TInfo : IVerification
        {
            dicError = new Dictionary <string, List <string> >();
            var map = VerifyMapCache.GetMap(info);

            foreach (var kic in map.ModelList)
            {
                var lstError = new List <string>();
                var value    = kic.Key.GetValue(info, null);

                // 是否必填
                if (kic.Value.Required != null && !kic.Value.Required.IsValid(value))
                {
                    lstError.Add(kic.Value.Required.ErrorMessage);
                    //dicError.Add(kic.Key.Name, lstError);
                }

                //if (value == null) { continue; }

                // 字符串长度判断
                if (kic.Value.StringLength != null && !kic.Value.StringLength.IsValid(value))
                {
                    lstError.Add(kic.Value.StringLength.ErrorMessage);
                }

                // 值的长度
                if (kic.Value.Range != null && !kic.Value.Range.IsValid(value))
                {
                    lstError.Add(kic.Value.Range.ErrorMessage);
                }

                // 正则
                if (kic.Value.RegularExpression != null && !kic.Value.RegularExpression.IsValid(value))
                {
                    lstError.Add(kic.Value.RegularExpression.ErrorMessage);
                }

                if (lstError.Count > 0)
                {
                    dicError.Add(kic.Key.Name, lstError);
                }
            }
            return(dicError.Count == 0);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     把提交过来的内容转化成为实体类(注意CheckBox 未选中时,是NULL,需要手动判断)
        /// </summary>
        /// <param name="request">NameValueCollection</param>
        /// <param name="prefix">控件前缀</param>
        /// <param name="dicError">返回错误消息,key:属性名称;value:错误消息</param>
        public static TInfo Fill <TInfo>(NameValueCollection request, out Dictionary <string, List <string> > dicError, string prefix = "hl") where TInfo : class, IVerification, new()
        {
            dicError = new Dictionary <string, List <string> >();
            var t = new TInfo();

            foreach (var kic in VerifyMapCache.GetMap(t).ModelList)
            {
                var lstError = new List <string>();
                var reqName  = prefix + kic.Key.Name;
                if (request[reqName] == null || !kic.Key.CanWrite)
                {
                    continue;
                }
                var obj = request[reqName].Trim().ConvertType(kic.Key.PropertyType);
                // 证明类型转换失败
                if (request[reqName].Trim().Length > 0 && obj == null)
                {
                    var type = kic.Key.PropertyType;
                    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        type = Nullable.GetUnderlyingType(type);
                    }
                    switch (type.Name)
                    {
                    case "DateTime":
                        lstError.Add("[" + kic.Value.Display.Name + "] 必须为时间格式。如:2011-09-01");
                        break;

                    case "Boolean":
                        lstError.Add("[" + kic.Value.Display.Name + "] 必须为布尔格式。如:false、true");
                        break;

                    case "UInt64":
                        lstError.Add("[" + kic.Value.Display.Name + "] 必须为整形格式。");
                        break;

                    case "Int64":
                        lstError.Add("[" + kic.Value.Display.Name + "] 必须为整形格式。");
                        break;

                    case "Int32":
                        lstError.Add("[" + kic.Value.Display.Name + "] 必须为整形格式2。");
                        break;

                    case "Int16":
                        lstError.Add("[" + kic.Value.Display.Name + "] 必须为整形格式。");
                        break;

                    case "Decimal":
                        lstError.Add("[" + kic.Value.Display.Name + "] 必须为数字格式。");
                        break;

                    case "Byte":
                        lstError.Add("[" + kic.Value.Display.Name + "] 必须为字节格式。");
                        break;

                    case "Long":
                        lstError.Add("[" + kic.Value.Display.Name + "] 必须为整形格式。");
                        break;

                    case "Float":
                        lstError.Add("[" + kic.Value.Display.Name + "] 必须为数字格式。");
                        break;

                    case "Double":
                        lstError.Add("[" + kic.Value.Display.Name + "] 必须为数字格式。");
                        break;
                    }
                }
                if (lstError.Count > 0)
                {
                    dicError.Add(kic.Key.Name, lstError);
                }
                else
                {
                    kic.Key.SetValue(t, obj, null);
                }
            }
            return(dicError.Count > 0 ? null : t);

            ;
        }