Пример #1
0
        /// <summary>
        /// 访问表示方法调用的节点
        /// </summary>
        /// <param name="node">方法调用节点</param>
        /// <param name="visitor">访问器</param>
        /// <returns></returns>
        public override Expression VisitMethodCall(MethodCallExpression node, ExpressionVisitorBase visitor)
        {
            Type   type       = node.Object != null ? node.Object.Type : node.Arguments[0].Type;
            string methodName = string.Empty;

            if (type == typeof(string) || node.Method.Name == "ToString")
            {
                methodName = "VisitStrMethodCall_";
            }
            else if (typeof(System.Collections.IEnumerable).IsAssignableFrom(type))
            {
                methodName = "VisitEnumMethodCall_";
            }

            if (!string.IsNullOrEmpty(methodName))
            {
                MemberAccess_Method invoker = null;
                MethodCallExressionVisitor._methods.TryGetValue(methodName + node.Method.Name, out invoker);
                if (invoker != null)
                {
                    object exp = invoker.Invoke(this, new object[] { node, visitor });
                    return(exp as Expression);
                }
            }

            throw new XfwException("{0}.{1} is not supported.", node.Method.DeclaringType, node.Method.Name);
        }
        /// <summary>
        /// 通过使用验证上下文、验证结果集合和用于指定是否验证所有属性的值,确定指定的对象是否有效。
        /// </summary>
        /// <param name="instance">要验证的对象</param>
        /// <param name="validationContext">用于描述要验证的对象的上下文</param>
        /// <param name="validationResults">用于包含每个失败的验证的集合</param>
        /// <param name="validateAllProperties">若要验证所有属性</param>
        /// <param name="breakOnFirstError">当第一个错误产生时,是否不再进行后续验证</param>
        /// <returns></returns>
        public static bool TryValidateObject(object instance, ValidationContext validationContext, ICollection <ValidationResult> validationResults, bool validateAllProperties, bool breakOnFirstError)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            // 如果验证所有的错误,则使用微软默认的校验
            if (!breakOnFirstError)
            {
                return(Validator.TryValidateObject(instance, validationContext, validationResults, validateAllProperties));
            }

            // 当第一个错误产生时,是否不再进行后续验证
            // ValidateObject 会验证所有,故可能需要重写
            IEnumerable collection = _ma_Method.Invoke(null, instance, validationContext, validateAllProperties, true) as IEnumerable;

            if (collection == null)
            {
                return(true);
            }

            bool        isValid  = true;
            IEnumerator iterator = collection.GetEnumerator();

            while (iterator.MoveNext())
            {
                isValid = false;
                Type type = iterator.Current.GetType();

                if (_pi == null)
                {
                    _pi          = type.GetProperty("ValidationResult", BindingFlags.Instance | BindingFlags.NonPublic);
                    _ma_Property = new MemberAccess_Method(_pi.GetGetMethod(true));
                }

                object           obj    = iterator.Current;
                ValidationResult result = (ValidationResult)_ma_Property.Invoke(iterator.Current);
                if (validationResults != null)
                {
                    validationResults.Add(result);
                }

                break;
            }

            return(isValid);
        }
Пример #3
0
        /// <summary>
        /// 访问表示字段或者属性的属性的节点 a.Name.Length
        /// </summary>
        /// <param name="node">字段或者属性节点</param>
        /// <param name="visitor">访问器</param>
        /// <returns></returns>
        public override Expression VisitMemberMember(MemberExpression node, ExpressionVisitorBase visitor)
        {
            Type   type       = node.Expression.Type;
            string methodName = string.Empty;

            if (type == typeof(string))
            {
                methodName = "VisitStrMember_";
            }

            if (!string.IsNullOrEmpty(methodName))
            {
                MemberAccess_Method invoker = null;
                MethodCallExressionVisitor._methods.TryGetValue(methodName + node.Member.Name, out invoker);
                if (invoker != null)
                {
                    object exp = invoker.Invoke(this, new object[] { node, visitor });
                    return(exp as Expression);
                }
            }

            throw new XfwException("{0}.{1} is not supported.", node.Member.DeclaringType, node.Member.Name);
        }
 static XValidator()
 {
     _mi        = typeof(Validator).GetMethod("GetObjectValidationErrors", BindingFlags.Static | BindingFlags.NonPublic);
     _ma_Method = new MemberAccess_Method(_mi);
 }