コード例 #1
0
ファイル: Validate.cs プロジェクト: xuhuo1234/FWindSoft
        public void WriteXml(XmlWriter writer)
        {
            string fullNmae = "";

            if (m_Validate != null)
            {
                fullNmae = m_Validate.GetType().FullName;
            }
            writer.WriteString(fullNmae);
            //writer.WriteEndElement();//必须写,序列化按流形式进行
        }
コード例 #2
0
        public override void ModifyOutput <OUTPUT>(OUTPUT output)
        {
            if (!(output is ICanBeValidated <OUTPUT>))
            {
                return;
            }

            var method        = _validate.GetType().GetMethod("SetupViewModel");
            var genericMethod = method.MakeGenericMethod(output.GetType());

            genericMethod.Invoke(_validate, new object[] { output });
        }
コード例 #3
0
        public override void PrepareInput <INPUT>(INPUT input)
        {
            if (!(input is ICanBeValidated <INPUT>))
            {
                return;
            }

            var method        = _validate.GetType().GetMethod("Validate");
            var genericMethod = method.MakeGenericMethod(input.GetType());

            IValidationResults <INPUT> validationResults = (IValidationResults <INPUT>)genericMethod.Invoke(_validate, new object[] { input });

            ((ICanBeValidated <INPUT>)input).ValidationResults.CloneFrom(validationResults);
        }
コード例 #4
0
        /// <summary>
        /// 校验对象属性值是否合法
        /// </summary>
        /// <param name="obj">待校验对象</param>
        /// <returns></returns>
        public static ValidateResult Validate(this IValidate obj)
        {
            ValidateResult result = new ValidateResult();

            PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo p in infos)
            {
                //获取数据校验特性。
                Attribute[] attrs = Attribute.GetCustomAttributes(p, typeof(ValidateAttribute), false);
                if (attrs.Length <= 0)
                {
                    continue;
                }

                //获取名称描述特性
                CaptionAttribute caption = Attribute.GetCustomAttribute(p, typeof(CaptionAttribute), false) as CaptionAttribute;
                object           value   = p.GetValue(obj);

                foreach (Attribute attr in attrs)
                {
                    ValidateAttribute validate = attr as ValidateAttribute;
                    if (validate == null)
                    {
                        continue;
                    }

                    result = Validate(validate, value, caption);
                    if (!result.IsSuccess)
                    {
                        return(result);
                    }
                }
            }
            return(result);
        }
コード例 #5
0
 public void Register(IValidate validator)
 {
     Context.Debug(() => $"Registering {validator.GetType().Name}.");
     Validators.Add(validator);
 }