コード例 #1
0
        /// <summary>
        /// 校验用户的信息是否合法
        /// </summary>
        /// <returns></returns>
        private static bool Validate(User user)
        {
            foreach (PropertyInfo propertyInfo in user.GetType().GetProperties())
            {
                if (propertyInfo.PropertyType!=typeof(string)) continue;

                //获取应用了RegexValidatorAtrribute特性的属性的特性
                var customAttribute = propertyInfo.GetCustomAttribute<RegexValidatorAtrribute>();

                //
                if (customAttribute!=null)
                {

                    bool match= Regex.IsMatch(propertyInfo.GetValue(user, null).ToString(), customAttribute.RegexPattern);

                    //customAttribute.ErrorMsg
                    if (!match) return match;
                }
            }

            return true;
        }