Esempio n. 1
0
        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="requestObj"></param>
        /// <returns></returns>
        public ResultWrap VerificationRequestData(Object requestObj)
        {
            ResultWrap rw      = null;
            Type       objType = null;

            FieldInfo[]    fieldInfos    = null;
            PropertyInfo[] propertyInfos = null;
            FieldVerificationAttribute[] fieldVerifiers = null;
            FieldVerificationAttribute   fieldVerifier  = null;
            String str = null;

            Object[] tmpAttrs = null;
            try
            {
                if (requestObj == null)
                {
                    throw new ArgumentNullException("requestObj对象不能为空!");
                }
                objType       = requestObj.GetType();
                propertyInfos = objType.GetProperties();
                fieldInfos    = objType.GetFields();
                if ((fieldInfos == null || fieldInfos.Length <= 0) && (propertyInfos == null || propertyInfos.Length <= 0))
                {
                    throw new ArgumentException("没有给业务对象添加字段!(" + requestObj.ToString() + ")");
                }
                if (fieldInfos != null)
                {
                    foreach (FieldInfo field in fieldInfos)
                    {
                        if (field.FieldType.BaseType == typeof(Array))
                        {
                            //数组
                            Object[] tmpObjs = (Object[])field.GetValue(requestObj);
                            foreach (Object tmpObj in tmpObjs)
                            {
                                rw = VerificationRequestData(tmpObj);
                                if (rw.SIGN == EnumResultFlagType.N)
                                {
                                    return(rw);
                                }
                            }
                        }
                        else
                        {
                            //非数组
                            //XmlIgnore属性不判断
                            tmpAttrs = field.GetCustomAttributes(typeof(XmlIgnoreAttribute), false);
                            if (tmpAttrs != null && tmpAttrs.Length > 0)
                            {
                                continue;
                            }
                            //非数组
                            if (field.FieldType.BaseType == typeof(System.Enum))
                            {
                                //类型为枚举的不判断
                                continue;
                            }


                            if (field.FieldType.BaseType == typeof(System.Enum))
                            {
                                //类型为枚举的不判断
                                continue;
                            }
                            fieldVerifiers = (FieldVerificationAttribute[])field.GetCustomAttributes(typeof(FieldVerificationAttribute), false);
                            if (fieldVerifiers == null || fieldVerifiers.Length <= 0)
                            {
                                throw new ArgumentException("没有给业务对象添加属性!" + requestObj.ToString() + "");
                            }
                            fieldVerifier = fieldVerifiers[0];
                            str           = field.GetValue(requestObj).ToString();
                            rw            = checkData(str, fieldVerifier, field);
                            if (rw.SIGN == EnumResultFlagType.N)
                            {
                                return(rw);
                            }
                        }
                    }
                }
                if (propertyInfos != null)
                {
                    foreach (PropertyInfo property in propertyInfos)
                    {
                        if (property.PropertyType.BaseType == typeof(Array))
                        {
                            //数组
                            Object[] tmpObjs = (Object[])property.GetValue(requestObj, null);
                            foreach (Object tmpObj in tmpObjs)
                            {
                                rw = VerificationRequestData(tmpObj);
                                if (rw.SIGN == EnumResultFlagType.N)
                                {
                                    return(rw);
                                }
                            }
                        }
                        else
                        {
                            //非数组
                            //XmlIgnore属性不判断
                            tmpAttrs = property.GetCustomAttributes(typeof(XmlIgnoreAttribute), false);
                            if (tmpAttrs != null && tmpAttrs.Length > 0)
                            {
                                continue;
                            }
                            //非数组
                            if (property.PropertyType.BaseType == typeof(System.Enum))
                            {
                                //类型为枚举的不判断
                                continue;
                            }
                            fieldVerifiers = (FieldVerificationAttribute[])property.GetCustomAttributes(typeof(FieldVerificationAttribute), false);
                            if (fieldVerifiers == null || fieldVerifiers.Length <= 0)
                            {
                                throw new ArgumentException("没有给业务对象添加属性!" + requestObj.ToString() + "");
                            }
                            fieldVerifier = fieldVerifiers[0];
                            str           = property.GetValue(requestObj, null).ToString();
                            rw            = checkData(str, fieldVerifier, property);
                            if (rw.SIGN == EnumResultFlagType.N)
                            {
                                return(rw);
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                rw = null;

                throw exc;
            }
            return(rw);
        }
Esempio n. 2
0
        private ResultWrap checkData(String str, FieldVerificationAttribute fieldVerifier, PropertyInfo property)
        {
            ResultWrap rw = null;

            Object[] tmpAttrs = null;
            try
            {
                tmpAttrs = property.GetCustomAttributes(typeof(XmlIgnoreAttribute), false);
                if (tmpAttrs != null && tmpAttrs.Length > 0)
                {
                    rw = new ResultWrap(EnumResultFlagType.Y, "");
                    return(rw);
                }

                int pos = -1;
                //判断是否是必填字段
                if (fieldVerifier.IsRequired)
                {
                    if (String.IsNullOrEmpty(str))
                    {
                        //空
                        rw = new ResultWrap(EnumResultFlagType.N, property.Name + "字段验证失败: 字段不能为空!");
                        return(rw);
                    }
                }
                //判断长度是否超长
                if (str.Length > fieldVerifier.FieldLength)
                {
                    rw = new ResultWrap(EnumResultFlagType.N, property.Name + "字段验证失败: 字段长度超过规则长度!");
                    return(rw);
                }
                //判断是否是数字
                if (fieldVerifier.IsNumberic && !String.IsNullOrEmpty(str))
                {
                    if (Regex.IsMatch(str, @"[^-0-9.]"))
                    {
                        //含有非数字
                        rw = new ResultWrap(EnumResultFlagType.N, property.Name + "字段验证失败: 字段不符合数字规则!");
                        return(rw);
                    }
                }
                //判断是否是金额
                if (fieldVerifier.IsDecimalDigit && !String.IsNullOrEmpty(str))
                {
                    pos = str.IndexOf('.');
                    if ((pos <= 0) || (str.Length - (pos + 1) != fieldVerifier.DecimalDigitsLength))
                    {
                        //不符合金额
                        rw = new ResultWrap(EnumResultFlagType.N, property.Name + "字段验证失败: 字段不符合数字规则!");
                        return(rw);
                    }
                }
                rw = new ResultWrap(EnumResultFlagType.Y, "");
            }
            catch (Exception exc)
            {
                rw = null;
                throw exc;
            }
            return(rw);
        }