Ejemplo n.º 1
0
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            CodePropertyReferenceExpression newParent = (CodePropertyReferenceExpression)expression;

            if (newParent.TargetObject == null)
            {
                ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullPropertyTarget, new object[] { newParent.PropertyName }), 0x53d);
                error.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error);
                return(null);
            }
            if (!validation.PushParentExpression(newParent))
            {
                return(null);
            }
            RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, newParent.TargetObject, false);

            validation.PopParentExpression();
            if (info == null)
            {
                return(null);
            }
            Type expressionType = info.ExpressionType;

            if (expressionType == null)
            {
                return(null);
            }
            if (expressionType == typeof(NullLiteral))
            {
                ValidationError error2 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullPropertyTarget, new object[] { newParent.PropertyName }), 0x546);
                error2.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error2);
                return(null);
            }
            bool         nonPublic    = false;
            BindingFlags bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;

            if (validation.AllowInternalMembers(expressionType))
            {
                bindingFlags |= BindingFlags.NonPublic;
                nonPublic     = true;
            }
            PropertyInfo item = validation.ResolveProperty(expressionType, newParent.PropertyName, bindingFlags);

            if (item == null)
            {
                ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownProperty, new object[] { newParent.PropertyName, RuleDecompiler.DecompileType(expressionType) }), 0x54a);
                error3.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error3);
                return(null);
            }
            if (item.PropertyType == null)
            {
                ValidationError error4 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, new object[] { newParent.PropertyName }), 0x194);
                error4.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error4);
                return(null);
            }
            MethodInfo accessorMethod = isWritten ? item.GetSetMethod(nonPublic) : item.GetGetMethod(nonPublic);

            if (accessorMethod == null)
            {
                string          format = isWritten ? Messages.UnknownPropertySet : Messages.UnknownPropertyGet;
                ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, format, new object[] { newParent.PropertyName, RuleDecompiler.DecompileType(expressionType) }), 0x54a);
                error5.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error5);
                return(null);
            }
            if (!validation.ValidateMemberAccess(newParent.TargetObject, expressionType, accessorMethod, newParent.PropertyName, newParent))
            {
                return(null);
            }
            object[] customAttributes = item.GetCustomAttributes(typeof(RuleAttribute), true);
            if ((customAttributes != null) && (customAttributes.Length > 0))
            {
                Stack <MemberInfo> stack = new Stack <MemberInfo>();
                stack.Push(item);
                bool flag2 = true;
                foreach (RuleAttribute attribute in customAttributes)
                {
                    if (!attribute.Validate(validation, item, expressionType, null))
                    {
                        flag2 = false;
                    }
                }
                stack.Pop();
                if (!flag2)
                {
                    return(null);
                }
            }
            return(new RulePropertyExpressionInfo(item, item.PropertyType, false));
        }
 internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
 {
     CodePropertyReferenceExpression newParent = (CodePropertyReferenceExpression) expression;
     if (newParent.TargetObject == null)
     {
         ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullPropertyTarget, new object[] { newParent.PropertyName }), 0x53d);
         error.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error);
         return null;
     }
     if (!validation.PushParentExpression(newParent))
     {
         return null;
     }
     RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, newParent.TargetObject, false);
     validation.PopParentExpression();
     if (info == null)
     {
         return null;
     }
     Type expressionType = info.ExpressionType;
     if (expressionType == null)
     {
         return null;
     }
     if (expressionType == typeof(NullLiteral))
     {
         ValidationError error2 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullPropertyTarget, new object[] { newParent.PropertyName }), 0x546);
         error2.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error2);
         return null;
     }
     bool nonPublic = false;
     BindingFlags bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
     if (validation.AllowInternalMembers(expressionType))
     {
         bindingFlags |= BindingFlags.NonPublic;
         nonPublic = true;
     }
     PropertyInfo item = validation.ResolveProperty(expressionType, newParent.PropertyName, bindingFlags);
     if (item == null)
     {
         ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownProperty, new object[] { newParent.PropertyName, RuleDecompiler.DecompileType(expressionType) }), 0x54a);
         error3.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error3);
         return null;
     }
     if (item.PropertyType == null)
     {
         ValidationError error4 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, new object[] { newParent.PropertyName }), 0x194);
         error4.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error4);
         return null;
     }
     MethodInfo accessorMethod = isWritten ? item.GetSetMethod(nonPublic) : item.GetGetMethod(nonPublic);
     if (accessorMethod == null)
     {
         string format = isWritten ? Messages.UnknownPropertySet : Messages.UnknownPropertyGet;
         ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, format, new object[] { newParent.PropertyName, RuleDecompiler.DecompileType(expressionType) }), 0x54a);
         error5.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error5);
         return null;
     }
     if (!validation.ValidateMemberAccess(newParent.TargetObject, expressionType, accessorMethod, newParent.PropertyName, newParent))
     {
         return null;
     }
     object[] customAttributes = item.GetCustomAttributes(typeof(RuleAttribute), true);
     if ((customAttributes != null) && (customAttributes.Length > 0))
     {
         Stack<MemberInfo> stack = new Stack<MemberInfo>();
         stack.Push(item);
         bool flag2 = true;
         foreach (RuleAttribute attribute in customAttributes)
         {
             if (!attribute.Validate(validation, item, expressionType, null))
             {
                 flag2 = false;
             }
         }
         stack.Pop();
         if (!flag2)
         {
             return null;
         }
     }
     return new RulePropertyExpressionInfo(item, item.PropertyType, false);
 }
Ejemplo n.º 3
0
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            string message;

            CodePropertyReferenceExpression propGetExpr = (CodePropertyReferenceExpression)expression;

            if (propGetExpr.TargetObject == null)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.NullPropertyTarget, propGetExpr.PropertyName);
                ValidationError error = new ValidationError(message, ErrorNumbers.Error_ParameterNotSet);
                error.UserData[RuleUserDataKeys.ErrorObject] = propGetExpr;
                validation.Errors.Add(error);
                return null;
            }

            // Early exit from this if a cycle is detected.
            if (!validation.PushParentExpression(propGetExpr))
                return null;

            RuleExpressionInfo targetExprInfo = RuleExpressionWalker.Validate(validation, propGetExpr.TargetObject, false);

            validation.PopParentExpression();

            if (targetExprInfo == null)     // error occurred, so simply return
                return null;

            Type targetType = targetExprInfo.ExpressionType;
            if (targetType == null)         // no type, so must have been an error already
                return null;

            if (targetType == typeof(NullLiteral))
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.NullPropertyTarget, propGetExpr.PropertyName);
                ValidationError error = new ValidationError(message, ErrorNumbers.Error_BindingTypeMissing);
                error.UserData[RuleUserDataKeys.ErrorObject] = propGetExpr;
                validation.Errors.Add(error);
                return null;
            }

            bool includeNonPublic = false;
            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;
            if (validation.AllowInternalMembers(targetType))
            {
                bindingFlags |= BindingFlags.NonPublic;
                includeNonPublic = true;
            }
            PropertyInfo pi = validation.ResolveProperty(targetType, propGetExpr.PropertyName, bindingFlags);

            if (pi == null)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.UnknownProperty, propGetExpr.PropertyName, RuleDecompiler.DecompileType(targetType));
                ValidationError error = new ValidationError(message, ErrorNumbers.Error_CannotResolveMember);
                error.UserData[RuleUserDataKeys.ErrorObject] = propGetExpr;
                validation.Errors.Add(error);
                return null;
            }

            if (pi.PropertyType == null)
            {
                // This can only happen with a design-time type.
                message = string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, propGetExpr.PropertyName);
                ValidationError error = new ValidationError(message, ErrorNumbers.Error_CouldNotDetermineMemberType);
                error.UserData[RuleUserDataKeys.ErrorObject] = propGetExpr;
                validation.Errors.Add(error);
                return null;
            }

            MethodInfo accessorMethod = isWritten ? pi.GetSetMethod(includeNonPublic) : pi.GetGetMethod(includeNonPublic);
            if (accessorMethod == null)
            {
                string baseMessage = isWritten ? Messages.UnknownPropertySet : Messages.UnknownPropertyGet;
                message = string.Format(CultureInfo.CurrentCulture, baseMessage, propGetExpr.PropertyName, RuleDecompiler.DecompileType(targetType));
                ValidationError error = new ValidationError(message, ErrorNumbers.Error_CannotResolveMember);
                error.UserData[RuleUserDataKeys.ErrorObject] = propGetExpr;
                validation.Errors.Add(error);
                return null;
            }

            if (!validation.ValidateMemberAccess(propGetExpr.TargetObject, targetType, accessorMethod, propGetExpr.PropertyName, propGetExpr))
                return null;

            // Validate any RuleAttributes, if present.
            object[] attrs = pi.GetCustomAttributes(typeof(RuleAttribute), true);
            if (attrs != null && attrs.Length > 0)
            {
                Stack<MemberInfo> methodStack = new Stack<MemberInfo>();
                methodStack.Push(pi);

                bool allAttributesValid = true;
                foreach (RuleAttribute ruleAttr in attrs)
                {
                    if (!ruleAttr.Validate(validation, pi, targetType, null))
                        allAttributesValid = false;
                }

                methodStack.Pop();

                if (!allAttributesValid)
                    return null;
            }

            return new RulePropertyExpressionInfo(pi, pi.PropertyType, false);
        }