public IList <string> GetErrorMessages(T entity, string fieldname)
        {
            List <string> list = new List <string>();

            if (UseAttributes)
            {
                var validationResults = new List <ValidationResult>();
                var isValid           = ValidateProperty(entity, fieldname, validationResults);

                if (validationResults.Count > 0)
                {
                    list.AddRange(validationResults.Select(v => v.ErrorMessage));
                }
            }

            if (_propertyRules != null && PropertyRules.ContainsKey(fieldname))
            {
                string msg = PropertyRules[fieldname].GetMessage(entity, Accessors[fieldname](entity));
                if (!string.IsNullOrEmpty(msg))
                {
                    list.Add(msg);
                }
            }

            return(list);
        }
        /// <summary>
        /// Some reflection and lambda wizardry to make this type safe...
        /// </summary>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="property"></param>
        /// <param name="rule"></param>
        /// <param name="message"></param>
        protected void AddPropertyRule <TProperty>(Expression <Func <T, TProperty> > property, Func <T, TProperty, bool> rule, string message)
        {
            MemberExpression memberExp = property.Body as MemberExpression;
            var info = memberExp.Member;
            var pd   = TypeDescriptor.CreateProperty(typeof(T), info.Name, typeof(TProperty));

            Func <T, object, bool> func = (entity, o) => rule(entity, (TProperty)pd.GetValue(entity));

            Accessors.Add(info.Name, e => pd.GetValue(e));
            PropertyRules.Add(info.Name, new PropertyRule <T>(func, message));
        }
        public bool IsValid(T entity, string fieldname)
        {
            bool isValid = true;

            if (UseAttributes)
            {
                var validationResults = new List <ValidationResult>();
                isValid = ValidateProperty(entity, fieldname, validationResults);
            }
            if (!isValid)
            {
                return(false);
            }

            if (_propertyRules != null && PropertyRules.ContainsKey(fieldname))
            {
                if (PropertyRules[fieldname].Rule(entity, Accessors[fieldname](entity)))
                {
                    return(false);
                }
            }
            return(isValid);
        }