internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            string message;
            ValidationError error = null;
            RulePropertyExpressionInfo propExprInfo = null;
            bool includeNonPublic = false;
            Type targetType = null;

            CodeIndexerExpression indexerExpr = (CodeIndexerExpression)expression;

            CodeExpression targetObject = indexerExpr.TargetObject;
            if (targetObject == null)
            {
                error = new ValidationError(Messages.NullIndexerTarget, ErrorNumbers.Error_ParameterNotSet);
                error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr;
                validation.Errors.Add(error);
                return null;
            }

            if (targetObject is CodeTypeReferenceExpression)
            {
                error = new ValidationError(Messages.IndexersCannotBeStatic, ErrorNumbers.Error_ParameterNotSet);
                error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr;
                validation.Errors.Add(error);
                return null;
            }

            if (indexerExpr.Indices == null || indexerExpr.Indices.Count == 0)
            {
                error = new ValidationError(Messages.MissingIndexExpressions, ErrorNumbers.Error_ParameterNotSet);
                error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr;
                validation.Errors.Add(error);
                return null;
            }

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

                RuleExpressionInfo targetExprInfo = RuleExpressionWalker.Validate(validation, indexerExpr.TargetObject, false);
                if (targetExprInfo == null)     // error occurred, so simply return
                    return null;

                targetType = targetExprInfo.ExpressionType;
                if (targetType == null)
                    return null;

                // if an error occurred (targetType == null), continue on to validate the arguments
                if (targetType == typeof(NullLiteral))
                {
                    message = string.Format(CultureInfo.CurrentCulture, Messages.NullIndexerTarget);
                    error = new ValidationError(message, ErrorNumbers.Error_ParameterNotSet);
                    error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr;
                    validation.Errors.Add(error);
                    targetType = null; // force exit after validating the arguments
                }

                List<CodeExpression> argExprs = new List<CodeExpression>();

                bool hasInvalidArgument = false;
                for (int i = 0; i < indexerExpr.Indices.Count; ++i)
                {
                    CodeExpression argExpr = indexerExpr.Indices[i];
                    if (argExpr == null)
                    {
                        error = new ValidationError(Messages.NullIndexExpression, ErrorNumbers.Error_ParameterNotSet);
                        error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr;
                        validation.Errors.Add(error);
                        hasInvalidArgument = true;
                    }
                    else
                    {
                        CodeDirectionExpression argDirection = argExpr as CodeDirectionExpression;
                        if (argDirection != null && argDirection.Direction != FieldDirection.In)
                        {
                            // No "ref" or "out" arguments are allowed on indexer arguments.
                            error = new ValidationError(Messages.IndexerArgCannotBeRefOrOut, ErrorNumbers.Error_IndexerArgCannotBeRefOrOut);
                            error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr;
                            validation.Errors.Add(error);
                            hasInvalidArgument = true;
                        }

                        if (argExpr is CodeTypeReferenceExpression)
                        {
                            message = string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, argExpr.GetType().FullName);
                            error = new ValidationError(message, ErrorNumbers.Error_CodeExpressionNotHandled);
                            error.UserData[RuleUserDataKeys.ErrorObject] = argExpr;
                            validation.AddError(error);
                            hasInvalidArgument = true;
                        }

                        // Validate the argument.
                        RuleExpressionInfo argExprInfo = RuleExpressionWalker.Validate(validation, argExpr, false);
                        if (argExprInfo == null)
                            hasInvalidArgument = true;
                        else
                            argExprs.Add(argExpr);
                    }
                }

                // Stop further validation if there was a problem with the target expression.
                if (targetType == null)
                    return null;

                // Stop further validation if there was a problem with any of the arguments.
                if (hasInvalidArgument)
                    return null;

                BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;
                if (validation.AllowInternalMembers(targetType))
                {
                    bindingFlags |= BindingFlags.NonPublic;
                    includeNonPublic = true;
                }

                // Everything okay so far, try to resolve the method.
                propExprInfo = validation.ResolveIndexerProperty(targetType, bindingFlags, argExprs, out error);
                if (propExprInfo == null)
                {
                    error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr;
                    validation.Errors.Add(error);
                    return null;
                }
            }
            finally
            {
                validation.PopParentExpression();
            }

            PropertyInfo pi = propExprInfo.PropertyInfo;

            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, pi.Name, RuleDecompiler.DecompileType(targetType));
                error = new ValidationError(message, ErrorNumbers.Error_CannotResolveMember);
                error.UserData[RuleUserDataKeys.ErrorObject] = indexerExpr;
                validation.Errors.Add(error);
                return null;
            }

            if (!validation.ValidateMemberAccess(targetObject, targetType, accessorMethod, pi.Name, indexerExpr))
                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, pi.GetIndexParameters()))
                        allAttributesValid = false;
                }

                methodStack.Pop();

                if (!allAttributesValid)
                    return null;
            }

            return propExprInfo;
        }
 internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
 {
     ValidationError item = null;
     RulePropertyExpressionInfo info = null;
     bool nonPublic = false;
     Type expressionType = null;
     CodeIndexerExpression newParent = (CodeIndexerExpression) expression;
     CodeExpression targetObject = newParent.TargetObject;
     if (targetObject == null)
     {
         item = new ValidationError(Messages.NullIndexerTarget, 0x53d);
         item.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(item);
         return null;
     }
     if (targetObject is CodeTypeReferenceExpression)
     {
         item = new ValidationError(Messages.IndexersCannotBeStatic, 0x53d);
         item.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(item);
         return null;
     }
     if ((newParent.Indices == null) || (newParent.Indices.Count == 0))
     {
         item = new ValidationError(Messages.MissingIndexExpressions, 0x53d);
         item.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(item);
         return null;
     }
     try
     {
         if (!validation.PushParentExpression(newParent))
         {
             return null;
         }
         RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, newParent.TargetObject, false);
         if (info2 == null)
         {
             return null;
         }
         expressionType = info2.ExpressionType;
         if (expressionType == null)
         {
             return null;
         }
         if (expressionType == typeof(NullLiteral))
         {
             item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullIndexerTarget, new object[0]), 0x53d);
             item.UserData["ErrorObject"] = newParent;
             validation.Errors.Add(item);
             expressionType = null;
         }
         List<CodeExpression> argumentExprs = new List<CodeExpression>();
         bool flag2 = false;
         for (int i = 0; i < newParent.Indices.Count; i++)
         {
             CodeExpression expression4 = newParent.Indices[i];
             if (expression4 == null)
             {
                 item = new ValidationError(Messages.NullIndexExpression, 0x53d);
                 item.UserData["ErrorObject"] = newParent;
                 validation.Errors.Add(item);
                 flag2 = true;
             }
             else
             {
                 CodeDirectionExpression expression5 = expression4 as CodeDirectionExpression;
                 if ((expression5 != null) && (expression5.Direction != FieldDirection.In))
                 {
                     item = new ValidationError(Messages.IndexerArgCannotBeRefOrOut, 0x19d);
                     item.UserData["ErrorObject"] = newParent;
                     validation.Errors.Add(item);
                     flag2 = true;
                 }
                 if (expression4 is CodeTypeReferenceExpression)
                 {
                     item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression4.GetType().FullName }), 0x548);
                     item.UserData["ErrorObject"] = expression4;
                     validation.AddError(item);
                     flag2 = true;
                 }
                 if (RuleExpressionWalker.Validate(validation, expression4, false) == null)
                 {
                     flag2 = true;
                 }
                 else
                 {
                     argumentExprs.Add(expression4);
                 }
             }
         }
         if (expressionType == null)
         {
             return null;
         }
         if (flag2)
         {
             return null;
         }
         BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;
         if (validation.AllowInternalMembers(expressionType))
         {
             bindingFlags |= BindingFlags.NonPublic;
             nonPublic = true;
         }
         info = validation.ResolveIndexerProperty(expressionType, bindingFlags, argumentExprs, out item);
         if (info == null)
         {
             item.UserData["ErrorObject"] = newParent;
             validation.Errors.Add(item);
             return null;
         }
     }
     finally
     {
         validation.PopParentExpression();
     }
     PropertyInfo propertyInfo = info.PropertyInfo;
     MethodInfo accessorMethod = isWritten ? propertyInfo.GetSetMethod(nonPublic) : propertyInfo.GetGetMethod(nonPublic);
     if (accessorMethod == null)
     {
         string format = isWritten ? Messages.UnknownPropertySet : Messages.UnknownPropertyGet;
         item = new ValidationError(string.Format(CultureInfo.CurrentCulture, format, new object[] { propertyInfo.Name, RuleDecompiler.DecompileType(expressionType) }), 0x54a);
         item.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(item);
         return null;
     }
     if (!validation.ValidateMemberAccess(targetObject, expressionType, accessorMethod, propertyInfo.Name, newParent))
     {
         return null;
     }
     object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(RuleAttribute), true);
     if ((customAttributes != null) && (customAttributes.Length > 0))
     {
         Stack<MemberInfo> stack = new Stack<MemberInfo>();
         stack.Push(propertyInfo);
         bool flag3 = true;
         foreach (RuleAttribute attribute in customAttributes)
         {
             if (!attribute.Validate(validation, propertyInfo, expressionType, propertyInfo.GetIndexParameters()))
             {
                 flag3 = false;
             }
         }
         stack.Pop();
         if (!flag3)
         {
             return null;
         }
     }
     return info;
 }
Beispiel #3
0
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            ValidationError            item = null;
            RulePropertyExpressionInfo info = null;
            bool nonPublic                     = false;
            Type expressionType                = null;
            CodeIndexerExpression newParent    = (CodeIndexerExpression)expression;
            CodeExpression        targetObject = newParent.TargetObject;

            if (targetObject == null)
            {
                item = new ValidationError(Messages.NullIndexerTarget, 0x53d);
                item.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(item);
                return(null);
            }
            if (targetObject is CodeTypeReferenceExpression)
            {
                item = new ValidationError(Messages.IndexersCannotBeStatic, 0x53d);
                item.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(item);
                return(null);
            }
            if ((newParent.Indices == null) || (newParent.Indices.Count == 0))
            {
                item = new ValidationError(Messages.MissingIndexExpressions, 0x53d);
                item.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(item);
                return(null);
            }
            try
            {
                if (!validation.PushParentExpression(newParent))
                {
                    return(null);
                }
                RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, newParent.TargetObject, false);
                if (info2 == null)
                {
                    return(null);
                }
                expressionType = info2.ExpressionType;
                if (expressionType == null)
                {
                    return(null);
                }
                if (expressionType == typeof(NullLiteral))
                {
                    item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullIndexerTarget, new object[0]), 0x53d);
                    item.UserData["ErrorObject"] = newParent;
                    validation.Errors.Add(item);
                    expressionType = null;
                }
                List <CodeExpression> argumentExprs = new List <CodeExpression>();
                bool flag2 = false;
                for (int i = 0; i < newParent.Indices.Count; i++)
                {
                    CodeExpression expression4 = newParent.Indices[i];
                    if (expression4 == null)
                    {
                        item = new ValidationError(Messages.NullIndexExpression, 0x53d);
                        item.UserData["ErrorObject"] = newParent;
                        validation.Errors.Add(item);
                        flag2 = true;
                    }
                    else
                    {
                        CodeDirectionExpression expression5 = expression4 as CodeDirectionExpression;
                        if ((expression5 != null) && (expression5.Direction != FieldDirection.In))
                        {
                            item = new ValidationError(Messages.IndexerArgCannotBeRefOrOut, 0x19d);
                            item.UserData["ErrorObject"] = newParent;
                            validation.Errors.Add(item);
                            flag2 = true;
                        }
                        if (expression4 is CodeTypeReferenceExpression)
                        {
                            item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression4.GetType().FullName }), 0x548);
                            item.UserData["ErrorObject"] = expression4;
                            validation.AddError(item);
                            flag2 = true;
                        }
                        if (RuleExpressionWalker.Validate(validation, expression4, false) == null)
                        {
                            flag2 = true;
                        }
                        else
                        {
                            argumentExprs.Add(expression4);
                        }
                    }
                }
                if (expressionType == null)
                {
                    return(null);
                }
                if (flag2)
                {
                    return(null);
                }
                BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;
                if (validation.AllowInternalMembers(expressionType))
                {
                    bindingFlags |= BindingFlags.NonPublic;
                    nonPublic     = true;
                }
                info = validation.ResolveIndexerProperty(expressionType, bindingFlags, argumentExprs, out item);
                if (info == null)
                {
                    item.UserData["ErrorObject"] = newParent;
                    validation.Errors.Add(item);
                    return(null);
                }
            }
            finally
            {
                validation.PopParentExpression();
            }
            PropertyInfo propertyInfo   = info.PropertyInfo;
            MethodInfo   accessorMethod = isWritten ? propertyInfo.GetSetMethod(nonPublic) : propertyInfo.GetGetMethod(nonPublic);

            if (accessorMethod == null)
            {
                string format = isWritten ? Messages.UnknownPropertySet : Messages.UnknownPropertyGet;
                item = new ValidationError(string.Format(CultureInfo.CurrentCulture, format, new object[] { propertyInfo.Name, RuleDecompiler.DecompileType(expressionType) }), 0x54a);
                item.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(item);
                return(null);
            }
            if (!validation.ValidateMemberAccess(targetObject, expressionType, accessorMethod, propertyInfo.Name, newParent))
            {
                return(null);
            }
            object[] customAttributes = propertyInfo.GetCustomAttributes(typeof(RuleAttribute), true);
            if ((customAttributes != null) && (customAttributes.Length > 0))
            {
                Stack <MemberInfo> stack = new Stack <MemberInfo>();
                stack.Push(propertyInfo);
                bool flag3 = true;
                foreach (RuleAttribute attribute in customAttributes)
                {
                    if (!attribute.Validate(validation, propertyInfo, expressionType, propertyInfo.GetIndexParameters()))
                    {
                        flag3 = false;
                    }
                }
                stack.Pop();
                if (!flag3)
                {
                    return(null);
                }
            }
            return(info);
        }