コード例 #1
0
        private static bool IsValid(MapContractAttribute mc, string value)
        {
            switch (mc.Type)
            {
            case FieldType.Number:
                return(string.IsNullOrEmpty(value) || value.IsNumber());

            case FieldType.Float:
                return(string.IsNullOrEmpty(value) || value.IsFloat());

            case FieldType.DateTime:
                return(string.IsNullOrEmpty(value) || value.IsDateTime(""));

            case FieldType.Boolean:
                return(string.IsNullOrEmpty(value) || value.IsBoolean());

            case FieldType.Custom:
            {
                return(Regex.IsMatch(value, mc.Regex, RegexOptions.IgnoreCase));
            }
            }
            return(true);
        }
コード例 #2
0
        public static List <Result> ValidateFields(object mp)
        {
            List <Result> results = new List <Result>();

            PropertyInfo[] properties = mp.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo property in properties)
            {
                object[]             mapContract = property.GetCustomAttributes(typeof(MapContractAttribute), false);
                object               value       = property.GetValue(mp, null);
                MapContractAttribute mc          = null;
                if (mapContract.Length > 0)
                {
                    mc = mapContract[0] as MapContractAttribute;
                    if ((value == null) && mc.IsRequired)
                    {
                        results.Add(new Result("必须字段未配置", string.Format("{0}字段不能为空,{1}", property.Name, mc.Describe), Level.Error, typeof(AppValidationManager)));
                    }
                    if (mc.PassValid)
                    {
                        continue;
                    }
                }
                if (property.PropertyType.IsGenericType && (property.PropertyType.GetGenericTypeDefinition() == typeof(List <>)))
                {
                    if (value != null)
                    {
                        int count = Convert.ToInt32(property.PropertyType.GetProperty("Count", BindingFlags.Public | BindingFlags.Instance).GetValue(value, null));
                        for (int i = 0; i < count; i++)
                        {
                            object obj = property.PropertyType.GetProperty("Item").GetValue(value, new object[] { i });
                            try
                            {
                                results.AddRange(ValidateFields(obj));
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
                else if (property.PropertyType.Module.Name.EqualIgnoreCase(typeof(MapContractAttribute).Assembly.ManifestModule.Name) && (value != null))
                {
                    try
                    {
                        results.AddRange(ValidateFields(value));
                    }
                    catch (Exception)
                    {
                    }
                }
                if (mc != null)
                {
                    value = (value == null) ? "" : value.ToString();
                    if (mc.EnumType != null)
                    {
                        int v;
                        //缓存枚举
                        if (!EnumDics.ContainsKey(mc.EnumType))
                        {
                            IDictionary <string, int> dic = new Dictionary <string, int>();
                            FieldInfo[] efields           = mc.EnumType.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
                            foreach (FieldInfo ef in efields)
                            {
                                object[] attrs = ef.GetCustomAttributes(typeof(XmlEnumAttribute), false);
                                if (attrs.Length > 0)
                                {
                                    XmlEnumAttribute xmlEnum = attrs[0] as XmlEnumAttribute;
                                    if (!string.IsNullOrEmpty(xmlEnum.Name))
                                    {
                                        v = (int)Enum.Parse(mc.EnumType, ef.GetValue(mp).ToString(), true);
                                        dic.Add(xmlEnum.Name.ToLower(), v);
                                    }
                                }
                            }
                            EnumDics.Add(mc.EnumType, dic);
                        }

                        if (mc.EnumValueType == EnumValueType.Value)
                        {
                            //如果配置值对应枚举的value
                            v = -1;
                            if (!int.TryParse(value.ToString(), out v))
                            {
                                results.Add(new Result("配置错误", string.Format("{0}字段值必须为数字,配置值:{1}。{2}", property.Name, value, mc.InvalidMessage), Level.Error, typeof(AppValidationManager)));
                            }
                            else if (Enum.GetName(mc.EnumType, v) == null)
                            {
                                results.Add(new Result("配置错误", string.Format("{0}字段值配置错误:{1}。{2}", property.Name, value, mc.InvalidMessage), Level.Error, typeof(AppValidationManager)));
                            }
                        }
                        else if (value.ToString().IsNotNullOrEmpty() && !EnumDics[mc.EnumType].ContainsKey(value.ToString().ToLower()))
                        {
                            //配置值对应枚举的text
                            results.Add(new Result("配置错误", string.Format("{0}字段值配置错误:{1}。{2}", property.Name, value, mc.InvalidMessage), Level.Error, typeof(AppValidationManager)));
                        }
                    }
                    else if (!IsValid(mc, value.ToString()))
                    {
                        if (mc.Type != FieldType.Custom)
                        {
                            results.Add(new Result("配置错误", string.Format("{0}字段应为{1}类型,配置值:{2}。{3}", new object[] { property.Name, mc.Type.ToString(), value, mc.InvalidMessage }), Level.Error, typeof(AppValidationManager)));
                        }
                        else
                        {
                            results.Add(new Result("配置错误", string.Format("{0}字段,配置值:{1}。{2}", new object[] { property.Name, value, mc.InvalidMessage }), Level.Error, typeof(AppValidationManager)));
                        }
                    }
                }
            }
            return(results);
        }