public ValidateAttributeInterfaceAttribute(ICustomValidatable validationObject)
 {
     _validationObject = validationObject;
 }
示例#2
0
 public ValidateAttributeInterfaceAttribute(ICustomValidatable validationObject)
 {
     _validationObject = validationObject;
 }
示例#3
0
文件: Validator.cs 项目: jrmrtns/cwat
        private static void DoValidate(object o, Type type, string groupname, ref ValidationSummary summary)
        {
            FieldInfo[] fields = type.GetFields(BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
            foreach (FieldInfo field in fields)
            {
                object[] attributes = field.GetCustomAttributes(true);
                foreach (Attribute attribute in attributes)
                {
                    IValidator validator = attribute as IValidator;
                    if (validator != null)
                    {
                        if (validator.IsInGroup(groupname))
                        {
                            object value;
                            string memberName;
                            try
                            {
                                value      = field.GetValue(o);
                                memberName = field.Name;
                            }
                            catch
                            {
                                break;
                            }
                            validator.IsValid(value, String.IsNullOrEmpty(validator.Alias) ? memberName : validator.Alias, ref summary);
                        }
                    }
                }
            }

            ICustomValidatable validatable = o as ICustomValidatable;

            PropertyInfo[] props = type.GetProperties(BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo prop in props)
            {
                object[] attributes = prop.GetCustomAttributes(true);
                foreach (Attribute attribute in attributes)
                {
                    IValidator validator = attribute as IValidator;
                    if (validator != null)
                    {
                        if (validator.IsInGroup(groupname))
                        {
                            object value;
                            string memberName;
                            try
                            {
                                value      = prop.GetValue(o, null);
                                memberName = prop.Name;
                            }
                            catch
                            {
                                break;
                            }

                            validator.IsValid(value, string.IsNullOrEmpty(validator.Alias) ? memberName : validator.Alias, ref summary);
                        }
                    }
                }
                if (validatable != null && type == o.GetType())
                {
                    string additionalValidation = validatable.ExecuteCustomValidation(prop.Name);
                    if (!string.IsNullOrEmpty(additionalValidation))
                    {
                        summary.Add(new ValidationEntry(false, additionalValidation, prop.Name, null));
                    }
                }
            }
        }