示例#1
0
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            Type expressionType = null;
            RuleMethodInvokeExpressionInfo info  = null;
            ValidationError            item      = null;
            BindingFlags               @public   = BindingFlags.Public;
            CodeMethodInvokeExpression newParent = (CodeMethodInvokeExpression)expression;

            if (isWritten)
            {
                item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeMethodInvokeExpression).ToString() }), 0x17a);
                item.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(item);
                return(null);
            }
            if ((newParent.Method == null) || (newParent.Method.TargetObject == null))
            {
                item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, new object[] { newParent.Method.MethodName }), 0x53d);
                item.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(item);
                return(null);
            }
            if ((newParent.Method.TypeArguments != null) && (newParent.Method.TypeArguments.Count > 0))
            {
                item = new ValidationError(Messages.GenericMethodsNotSupported, 0x548);
                item.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(item);
                return(null);
            }
            try
            {
                if (!validation.PushParentExpression(newParent))
                {
                    return(null);
                }
                RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, newParent.Method.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.NullMethodTarget, new object[] { newParent.Method.MethodName }), 0x546);
                    item.UserData["ErrorObject"] = newParent;
                    validation.Errors.Add(item);
                    expressionType = null;
                }
                List <CodeExpression> argumentExprs = new List <CodeExpression>();
                bool flag = false;
                if (newParent.Parameters != null)
                {
                    for (int i = 0; i < newParent.Parameters.Count; i++)
                    {
                        CodeExpression expression3 = newParent.Parameters[i];
                        if (expression3 == null)
                        {
                            item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodParameter, new object[] { i.ToString(CultureInfo.CurrentCulture), newParent.Method.MethodName }), 0x53d);
                            item.UserData["ErrorObject"] = newParent;
                            validation.Errors.Add(item);
                            expressionType = null;
                        }
                        else
                        {
                            if (expression3 is CodeTypeReferenceExpression)
                            {
                                item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression3.GetType().FullName }), 0x548);
                                item.UserData["ErrorObject"] = expression3;
                                validation.AddError(item);
                                flag = true;
                            }
                            if (RuleExpressionWalker.Validate(validation, expression3, false) == null)
                            {
                                flag = true;
                            }
                            argumentExprs.Add(expression3);
                        }
                    }
                }
                if (expressionType == null)
                {
                    return(null);
                }
                if (flag)
                {
                    return(null);
                }
                if (newParent.Method.TargetObject is CodeTypeReferenceExpression)
                {
                    @public |= BindingFlags.FlattenHierarchy | BindingFlags.Static;
                }
                else
                {
                    @public |= BindingFlags.Instance;
                }
                if (validation.AllowInternalMembers(expressionType))
                {
                    @public |= BindingFlags.NonPublic;
                }
                info = validation.ResolveMethod(expressionType, newParent.Method.MethodName, @public, argumentExprs, out item);
                if ((info == null) && newParent.UserData.Contains("QualifiedName"))
                {
                    string qualifiedName = newParent.UserData["QualifiedName"] as string;
                    Type   type2         = validation.ResolveType(qualifiedName);
                    if (type2 != null)
                    {
                        validation.DetermineExtensionMethods(type2.Assembly);
                        info = validation.ResolveMethod(expressionType, newParent.Method.MethodName, @public, argumentExprs, out item);
                    }
                }
                if (info == null)
                {
                    item.UserData["ErrorObject"] = newParent;
                    validation.Errors.Add(item);
                    return(null);
                }
            }
            finally
            {
                validation.PopParentExpression();
            }
            MethodInfo methodInfo = info.MethodInfo;

            if (methodInfo.ReturnType == null)
            {
                item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, new object[] { newParent.Method.MethodName }), 0x194);
                item.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(item);
                return(null);
            }
            if (!validation.ValidateMemberAccess(newParent.Method.TargetObject, expressionType, methodInfo, newParent.Method.MethodName, newParent))
            {
                return(null);
            }
            object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuleAttribute), true);
            if ((customAttributes != null) && (customAttributes.Length > 0))
            {
                Stack <MemberInfo> stack = new Stack <MemberInfo>();
                stack.Push(methodInfo);
                bool flag2 = true;
                foreach (RuleAttribute attribute in customAttributes)
                {
                    if (!attribute.Validate(validation, methodInfo, expressionType, methodInfo.GetParameters()))
                    {
                        flag2 = false;
                    }
                }
                stack.Pop();
                if (!flag2)
                {
                    return(null);
                }
            }
            if (methodInfo is ExtensionMethodInfo)
            {
                newParent.UserData["QualifiedName"] = methodInfo.DeclaringType.AssemblyQualifiedName;
            }
            return(info);
        }
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            ValidationError            error;
            CodeObjectCreateExpression newParent = (CodeObjectCreateExpression)expression;

            if (isWritten)
            {
                error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeObjectCreateExpression).ToString() }), 0x17a);
                error.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error);
                return(null);
            }
            if (newParent.CreateType == null)
            {
                error = new ValidationError(Messages.NullTypeType, 0x53d);
                error.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error);
                return(null);
            }
            Type type = validation.ResolveType(newParent.CreateType);

            if (type == null)
            {
                return(null);
            }
            List <CodeExpression> argumentExprs = new List <CodeExpression>();

            try
            {
                if (!validation.PushParentExpression(newParent))
                {
                    return(null);
                }
                bool flag = false;
                for (int i = 0; i < newParent.Parameters.Count; i++)
                {
                    CodeExpression expression3 = newParent.Parameters[i];
                    if (expression3 == null)
                    {
                        error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullConstructorParameter, new object[] { i.ToString(CultureInfo.CurrentCulture), RuleDecompiler.DecompileType(type) }), 0x53d);
                        error.UserData["ErrorObject"] = newParent;
                        validation.Errors.Add(error);
                        flag = true;
                    }
                    else
                    {
                        if (RuleExpressionWalker.Validate(validation, expression3, false) == null)
                        {
                            flag = true;
                        }
                        argumentExprs.Add(expression3);
                    }
                }
                if (flag)
                {
                    return(null);
                }
            }
            finally
            {
                validation.PopParentExpression();
            }
            BindingFlags constructorBindingFlags = BindingFlags.Public | BindingFlags.Instance;

            if (validation.AllowInternalMembers(type))
            {
                constructorBindingFlags |= BindingFlags.NonPublic;
            }
            if (type.IsValueType && (argumentExprs.Count == 0))
            {
                return(new RuleExpressionInfo(type));
            }
            if (type.IsAbstract)
            {
                error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownConstructor, new object[] { RuleDecompiler.DecompileType(type) }), 0x137);
                error.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error);
                return(null);
            }
            RuleConstructorExpressionInfo info2 = validation.ResolveConstructor(type, constructorBindingFlags, argumentExprs, out error);

            if (info2 == null)
            {
                error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownConstructor, new object[] { RuleDecompiler.DecompileType(type) }), 0x137);
                error.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error);
                return(null);
            }
            return(info2);
        }
 public override bool Validate(RuleValidation validator)
 {
     if (validator == null)
     {
         throw new ArgumentNullException("validator");
     }
     bool flag = true;
     if (this.path == null)
     {
         ValidationError error = new ValidationError(Messages.NullUpdate, 0x53d);
         error.UserData["ErrorObject"] = this;
         validator.AddError(error);
         flag = false;
     }
     string[] strArray = this.path.Split(new char[] { '/' });
     if (strArray[0] == "this")
     {
         Type thisType = validator.ThisType;
         for (int i = 1; i < strArray.Length; i++)
         {
             if (strArray[i] == "*")
             {
                 if (i < (strArray.Length - 1))
                 {
                     ValidationError error2 = new ValidationError(Messages.InvalidWildCardInPathQualifier, 0x195);
                     error2.UserData["ErrorObject"] = this;
                     validator.AddError(error2);
                     flag = false;
                 }
                 return flag;
             }
             if (!string.IsNullOrEmpty(strArray[i]) || (i != (strArray.Length - 1)))
             {
                 goto Label_00ED;
             }
             return flag;
         Label_00E6:
             thisType = thisType.GetElementType();
         Label_00ED:
             if (thisType.IsArray)
             {
                 goto Label_00E6;
             }
             BindingFlags bindingAttr = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
             if (validator.AllowInternalMembers(thisType))
             {
                 bindingAttr |= BindingFlags.NonPublic;
             }
             FieldInfo field = thisType.GetField(strArray[i], bindingAttr);
             if (field != null)
             {
                 thisType = field.FieldType;
             }
             else
             {
                 PropertyInfo property = thisType.GetProperty(strArray[i], bindingAttr);
                 if (property != null)
                 {
                     thisType = property.PropertyType;
                 }
                 else
                 {
                     ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, new object[] { strArray[i] }), 0x57b);
                     error3.UserData["ErrorObject"] = this;
                     validator.AddError(error3);
                     return false;
                 }
             }
         }
         return flag;
     }
     ValidationError error4 = new ValidationError(Messages.UpdateNotThis, 0x57b);
     error4.UserData["ErrorObject"] = this;
     validator.AddError(error4);
     return false;
 }
        internal override bool Validate(RuleValidation validation, MemberInfo member, Type contextType, ParameterInfo[] parameters)
        {
            ValidationError error = null;

            if (string.IsNullOrEmpty(this.attributePath))
            {
                return(true);
            }
            string[] strArray = this.attributePath.Split(new char[] { '/' });
            string   str2     = strArray[0];
            int      num      = 0;

            if (this.attributeTarget == RuleAttributeTarget.This)
            {
                if (str2 == "this")
                {
                    num++;
                }
            }
            else
            {
                bool flag2 = false;
                for (int j = 0; j < parameters.Length; j++)
                {
                    ParameterInfo info = parameters[j];
                    if (info.Name == str2)
                    {
                        flag2       = true;
                        contextType = info.ParameterType;
                        break;
                    }
                }
                if (!flag2)
                {
                    error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleAttributeParameter, new object[] { str2, member.Name }), 420);
                    error.UserData["ErrorObject"] = this;
                    validation.AddError(error);
                    return(false);
                }
                num++;
            }
            int    length = strArray.Length;
            string str3   = strArray[length - 1];

            if (string.IsNullOrEmpty(str3) || (str3 == "*"))
            {
                length--;
            }
            Type fieldType = contextType;

            for (int i = num; i < length; i++)
            {
                if (!(strArray[i] == "*"))
                {
                    goto Label_0171;
                }
                error = new ValidationError(Messages.InvalidWildCardInPathQualifier, 0x195);
                error.UserData["ErrorObject"] = this;
                validation.AddError(error);
                return(false);

Label_0168:
                fieldType = fieldType.GetElementType();
Label_0171:
                if (fieldType.IsArray)
                {
                    goto Label_0168;
                }
                BindingFlags bindingAttr = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
                if (validation.AllowInternalMembers(fieldType))
                {
                    bindingAttr |= BindingFlags.NonPublic;
                }
                FieldInfo field = fieldType.GetField(strArray[i], bindingAttr);
                if (field != null)
                {
                    fieldType = field.FieldType;
                }
                else
                {
                    PropertyInfo property = fieldType.GetProperty(strArray[i], bindingAttr);
                    if (property != null)
                    {
                        fieldType = property.PropertyType;
                    }
                    else
                    {
                        error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, new object[] { strArray[i] }), 390);
                        error.UserData["ErrorObject"] = this;
                        validation.AddError(error);
                        return(false);
                    }
                }
            }
            return(true);
        }
        public override bool Validate(RuleValidation validator)
        {
            if (validator == null)
            {
                throw new ArgumentNullException("validator");
            }
            bool flag = true;

            if (this.path == null)
            {
                ValidationError error = new ValidationError(Messages.NullUpdate, 0x53d);
                error.UserData["ErrorObject"] = this;
                validator.AddError(error);
                flag = false;
            }
            string[] strArray = this.path.Split(new char[] { '/' });
            if (strArray[0] == "this")
            {
                Type thisType = validator.ThisType;
                for (int i = 1; i < strArray.Length; i++)
                {
                    if (strArray[i] == "*")
                    {
                        if (i < (strArray.Length - 1))
                        {
                            ValidationError error2 = new ValidationError(Messages.InvalidWildCardInPathQualifier, 0x195);
                            error2.UserData["ErrorObject"] = this;
                            validator.AddError(error2);
                            flag = false;
                        }
                        return(flag);
                    }
                    if (!string.IsNullOrEmpty(strArray[i]) || (i != (strArray.Length - 1)))
                    {
                        goto Label_00ED;
                    }
                    return(flag);

Label_00E6:
                    thisType = thisType.GetElementType();
Label_00ED:
                    if (thisType.IsArray)
                    {
                        goto Label_00E6;
                    }
                    BindingFlags bindingAttr = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
                    if (validator.AllowInternalMembers(thisType))
                    {
                        bindingAttr |= BindingFlags.NonPublic;
                    }
                    FieldInfo field = thisType.GetField(strArray[i], bindingAttr);
                    if (field != null)
                    {
                        thisType = field.FieldType;
                    }
                    else
                    {
                        PropertyInfo property = thisType.GetProperty(strArray[i], bindingAttr);
                        if (property != null)
                        {
                            thisType = property.PropertyType;
                        }
                        else
                        {
                            ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, new object[] { strArray[i] }), 0x57b);
                            error3.UserData["ErrorObject"] = this;
                            validator.AddError(error3);
                            return(false);
                        }
                    }
                }
                return(flag);
            }
            ValidationError error4 = new ValidationError(Messages.UpdateNotThis, 0x57b);

            error4.UserData["ErrorObject"] = this;
            validator.AddError(error4);
            return(false);
        }
        public override bool Validate(RuleValidation validator)
        {
            if (validator == null)
                throw new ArgumentNullException("validator");

            bool success = true;

            if (path == null)
            {
                ValidationError error = new ValidationError(Messages.NullUpdate, ErrorNumbers.Error_ParameterNotSet);
                error.UserData[RuleUserDataKeys.ErrorObject] = this;
                validator.AddError(error);
                success = false;
            }

            // now make sure that the path is valid
            string[] parts = path.Split('/');
            if (parts[0] == "this")
            {
                Type currentType = validator.ThisType;
                for (int i = 1; i < parts.Length; ++i)
                {
                    if (parts[i] == "*")
                    {
                        if (i < parts.Length - 1)
                        {
                            // The "*" occurred in the middle of the path, which is a no-no.
                            ValidationError error = new ValidationError(Messages.InvalidWildCardInPathQualifier, ErrorNumbers.Error_InvalidWildCardInPathQualifier);
                            error.UserData[RuleUserDataKeys.ErrorObject] = this;
                            validator.AddError(error);
                            success = false;
                            break;
                        }
                        else
                        {
                            // It occurred at the end, which is okay.
                            break;
                        }
                    }
                    else if (string.IsNullOrEmpty(parts[i]) && i == parts.Length - 1)
                    {
                        // It's okay to end with a "/".
                        break;
                    }

                    while (currentType.IsArray)
                        currentType = currentType.GetElementType();

                    BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;
                    if (validator.AllowInternalMembers(currentType))
                        bindingFlags |= BindingFlags.NonPublic;
                    FieldInfo field = currentType.GetField(parts[i], bindingFlags);
                    if (field != null)
                    {
                        currentType = field.FieldType;
                    }
                    else
                    {
                        PropertyInfo property = currentType.GetProperty(parts[i], bindingFlags);
                        if (property != null)
                        {
                            currentType = property.PropertyType;
                        }
                        else
                        {
                            string message = string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, parts[i]);
                            ValidationError error = new ValidationError(message, ErrorNumbers.Error_InvalidUpdate);
                            error.UserData[RuleUserDataKeys.ErrorObject] = this;
                            validator.AddError(error);
                            success = false;
                            break;
                        }
                    }
                }
            }
            else
            {
                ValidationError error = new ValidationError(Messages.UpdateNotThis, ErrorNumbers.Error_InvalidUpdate);
                error.UserData[RuleUserDataKeys.ErrorObject] = this;
                validator.AddError(error);
                success = false;
            }

            return success;
        }
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            string message;
            ValidationError error;

            CodeObjectCreateExpression createExpression = (CodeObjectCreateExpression)expression;

            if (isWritten)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, typeof(CodeObjectCreateExpression).ToString());
                error = new ValidationError(message, ErrorNumbers.Error_InvalidAssignTarget);
                error.UserData[RuleUserDataKeys.ErrorObject] = createExpression;
                validation.Errors.Add(error);
                return null;
            }

            if (createExpression.CreateType == null)
            {
                error = new ValidationError(Messages.NullTypeType, ErrorNumbers.Error_ParameterNotSet);
                error.UserData[RuleUserDataKeys.ErrorObject] = createExpression;
                validation.Errors.Add(error);
                return null;
            }

            Type resultType = validation.ResolveType(createExpression.CreateType);
            if (resultType == null)
                return null;

            // look up parameters
            List<CodeExpression> parameters = new List<CodeExpression>();
            try
            {
                // Early exit from this if a cycle is detected.
                if (!validation.PushParentExpression(createExpression))
                    return null;

                bool hasInvalidArgument = false;
                for (int i = 0; i < createExpression.Parameters.Count; ++i)
                {
                    CodeExpression parameter = createExpression.Parameters[i];
                    if (parameter == null)
                    {
                        message = string.Format(CultureInfo.CurrentCulture, Messages.NullConstructorParameter, i.ToString(CultureInfo.CurrentCulture), RuleDecompiler.DecompileType(resultType));
                        error = new ValidationError(message, ErrorNumbers.Error_ParameterNotSet);
                        error.UserData[RuleUserDataKeys.ErrorObject] = createExpression;
                        validation.Errors.Add(error);
                        hasInvalidArgument = true;
                    }
                    else
                    {
                        RuleExpressionInfo parameterInfo = RuleExpressionWalker.Validate(validation, parameter, false);
                        if (parameterInfo == null)
                            hasInvalidArgument = true;
                        parameters.Add(parameter);
                    }
                }
                // quit if parameters not valid
                if (hasInvalidArgument)
                    return null;
            }
            finally
            {
                validation.PopParentExpression();
            }

            // see if we can find the matching constructor
            BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance;
            if (validation.AllowInternalMembers(resultType))
                bindingFlags |= BindingFlags.NonPublic;

            // creating a value-type object with no parameters can always be done
            if ((resultType.IsValueType) && (parameters.Count == 0))
                return new RuleExpressionInfo(resultType);

            // error if type is an abstract type
            if (resultType.IsAbstract)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.UnknownConstructor, RuleDecompiler.DecompileType(resultType));
                error = new ValidationError(message, ErrorNumbers.Error_MethodNotExists);
                error.UserData[RuleUserDataKeys.ErrorObject] = createExpression;
                validation.Errors.Add(error);
                return null;
            }

            RuleConstructorExpressionInfo constructorInvokeInfo = validation.ResolveConstructor(resultType, bindingFlags, parameters, out error);
            if (constructorInvokeInfo == null)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.UnknownConstructor, RuleDecompiler.DecompileType(resultType));
                error = new ValidationError(message, ErrorNumbers.Error_MethodNotExists);
                error.UserData[RuleUserDataKeys.ErrorObject] = createExpression;
                validation.Errors.Add(error);
                return null;
            }

            return constructorInvokeInfo;
        }
 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)
        {
            Type targetType = null;
            RuleMethodInvokeExpressionInfo methodInvokeInfo = null;
            string message;
            ValidationError error = null;
            BindingFlags bindingFlags = BindingFlags.Public;

            CodeMethodInvokeExpression invokeExpr = (CodeMethodInvokeExpression)expression;

            if (isWritten)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, typeof(CodeMethodInvokeExpression).ToString());
                error = new ValidationError(message, ErrorNumbers.Error_InvalidAssignTarget);
                error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr;
                validation.Errors.Add(error);
                return null;
            }

            if ((invokeExpr.Method == null) || (invokeExpr.Method.TargetObject == null))
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, invokeExpr.Method.MethodName);
                error = new ValidationError(message, ErrorNumbers.Error_ParameterNotSet);
                error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr;
                validation.Errors.Add(error);
                return null; // Fatal error; discontinue validation of this object.
            }

            if ((invokeExpr.Method.TypeArguments != null) && (invokeExpr.Method.TypeArguments.Count > 0))
            {
                error = new ValidationError(Messages.GenericMethodsNotSupported, ErrorNumbers.Error_CodeExpressionNotHandled);
                error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr;
                validation.Errors.Add(error);
                return null;
            }

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

                RuleExpressionInfo targetExprInfo = RuleExpressionWalker.Validate(validation, invokeExpr.Method.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.NullMethodTarget, invokeExpr.Method.MethodName);
                    error = new ValidationError(message, ErrorNumbers.Error_BindingTypeMissing);
                    error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr;
                    validation.Errors.Add(error);
                    targetType = null; // force exit after validating the arguments
                }

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

                bool hasInvalidArgument = false;
                if (invokeExpr.Parameters != null)
                {
                    for (int i = 0; i < invokeExpr.Parameters.Count; ++i)
                    {
                        CodeExpression argExpr = invokeExpr.Parameters[i];
                        if (argExpr == null)
                        {
                            message = string.Format(CultureInfo.CurrentCulture, Messages.NullMethodParameter, i.ToString(CultureInfo.CurrentCulture), invokeExpr.Method.MethodName);
                            error = new ValidationError(message, ErrorNumbers.Error_ParameterNotSet);
                            error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr;
                            validation.Errors.Add(error);
                            targetType = null; // force exit after validating the rest of the arguments
                        }
                        else
                        {
                            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;
                            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;

                if (invokeExpr.Method.TargetObject is CodeTypeReferenceExpression)
                    bindingFlags |= BindingFlags.Static | BindingFlags.FlattenHierarchy;
                else
                    bindingFlags |= BindingFlags.Instance;
                if (validation.AllowInternalMembers(targetType))
                    bindingFlags |= BindingFlags.NonPublic;

                // Everything okay so far, try to resolve the method.
                methodInvokeInfo = validation.ResolveMethod(targetType, invokeExpr.Method.MethodName, bindingFlags, argExprs, out error);
                if ((methodInvokeInfo == null) && (invokeExpr.UserData.Contains(RuleUserDataKeys.QualifiedName)))
                {
                    // failed to resolve the method, but a fully qualified type name is around
                    // load the type, add it to the assemblies, and try again
                    string qualifiedName = invokeExpr.UserData[RuleUserDataKeys.QualifiedName] as string;
                    Type containingClassType = validation.ResolveType(qualifiedName);
                    if (containingClassType != null)
                    {
                        validation.DetermineExtensionMethods(containingClassType.Assembly);
                        methodInvokeInfo = validation.ResolveMethod(targetType, invokeExpr.Method.MethodName, bindingFlags, argExprs, out error);
                    }
                }
                if (methodInvokeInfo == null)
                {
                    error.UserData[RuleUserDataKeys.ErrorObject] = invokeExpr;
                    validation.Errors.Add(error);
                    return null;
                }
            }
            finally
            {
                validation.PopParentExpression();
            }


            MethodInfo mi = methodInvokeInfo.MethodInfo;

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

            if (!validation.ValidateMemberAccess(invokeExpr.Method.TargetObject, targetType, mi, invokeExpr.Method.MethodName, invokeExpr))
                return null;

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

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

                methodStack.Pop();

                if (!allAttributesValid)
                    return null;
            }

            // if this is an extension method, save the type information
            if (mi is ExtensionMethodInfo)
            {
                invokeExpr.UserData[RuleUserDataKeys.QualifiedName] = mi.DeclaringType.AssemblyQualifiedName;
            }

            return methodInvokeInfo;
        }
示例#10
0
        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;
        }
示例#11
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);
        }
 internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
 {
     Type expressionType = null;
     RuleMethodInvokeExpressionInfo info = null;
     ValidationError item = null;
     BindingFlags @public = BindingFlags.Public;
     CodeMethodInvokeExpression newParent = (CodeMethodInvokeExpression) expression;
     if (isWritten)
     {
         item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeMethodInvokeExpression).ToString() }), 0x17a);
         item.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(item);
         return null;
     }
     if ((newParent.Method == null) || (newParent.Method.TargetObject == null))
     {
         item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, new object[] { newParent.Method.MethodName }), 0x53d);
         item.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(item);
         return null;
     }
     if ((newParent.Method.TypeArguments != null) && (newParent.Method.TypeArguments.Count > 0))
     {
         item = new ValidationError(Messages.GenericMethodsNotSupported, 0x548);
         item.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(item);
         return null;
     }
     try
     {
         if (!validation.PushParentExpression(newParent))
         {
             return null;
         }
         RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, newParent.Method.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.NullMethodTarget, new object[] { newParent.Method.MethodName }), 0x546);
             item.UserData["ErrorObject"] = newParent;
             validation.Errors.Add(item);
             expressionType = null;
         }
         List<CodeExpression> argumentExprs = new List<CodeExpression>();
         bool flag = false;
         if (newParent.Parameters != null)
         {
             for (int i = 0; i < newParent.Parameters.Count; i++)
             {
                 CodeExpression expression3 = newParent.Parameters[i];
                 if (expression3 == null)
                 {
                     item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodParameter, new object[] { i.ToString(CultureInfo.CurrentCulture), newParent.Method.MethodName }), 0x53d);
                     item.UserData["ErrorObject"] = newParent;
                     validation.Errors.Add(item);
                     expressionType = null;
                 }
                 else
                 {
                     if (expression3 is CodeTypeReferenceExpression)
                     {
                         item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression3.GetType().FullName }), 0x548);
                         item.UserData["ErrorObject"] = expression3;
                         validation.AddError(item);
                         flag = true;
                     }
                     if (RuleExpressionWalker.Validate(validation, expression3, false) == null)
                     {
                         flag = true;
                     }
                     argumentExprs.Add(expression3);
                 }
             }
         }
         if (expressionType == null)
         {
             return null;
         }
         if (flag)
         {
             return null;
         }
         if (newParent.Method.TargetObject is CodeTypeReferenceExpression)
         {
             @public |= BindingFlags.FlattenHierarchy | BindingFlags.Static;
         }
         else
         {
             @public |= BindingFlags.Instance;
         }
         if (validation.AllowInternalMembers(expressionType))
         {
             @public |= BindingFlags.NonPublic;
         }
         info = validation.ResolveMethod(expressionType, newParent.Method.MethodName, @public, argumentExprs, out item);
         if ((info == null) && newParent.UserData.Contains("QualifiedName"))
         {
             string qualifiedName = newParent.UserData["QualifiedName"] as string;
             Type type2 = validation.ResolveType(qualifiedName);
             if (type2 != null)
             {
                 validation.DetermineExtensionMethods(type2.Assembly);
                 info = validation.ResolveMethod(expressionType, newParent.Method.MethodName, @public, argumentExprs, out item);
             }
         }
         if (info == null)
         {
             item.UserData["ErrorObject"] = newParent;
             validation.Errors.Add(item);
             return null;
         }
     }
     finally
     {
         validation.PopParentExpression();
     }
     MethodInfo methodInfo = info.MethodInfo;
     if (methodInfo.ReturnType == null)
     {
         item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, new object[] { newParent.Method.MethodName }), 0x194);
         item.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(item);
         return null;
     }
     if (!validation.ValidateMemberAccess(newParent.Method.TargetObject, expressionType, methodInfo, newParent.Method.MethodName, newParent))
     {
         return null;
     }
     object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuleAttribute), true);
     if ((customAttributes != null) && (customAttributes.Length > 0))
     {
         Stack<MemberInfo> stack = new Stack<MemberInfo>();
         stack.Push(methodInfo);
         bool flag2 = true;
         foreach (RuleAttribute attribute in customAttributes)
         {
             if (!attribute.Validate(validation, methodInfo, expressionType, methodInfo.GetParameters()))
             {
                 flag2 = false;
             }
         }
         stack.Pop();
         if (!flag2)
         {
             return null;
         }
     }
     if (methodInfo is ExtensionMethodInfo)
     {
         newParent.UserData["QualifiedName"] = methodInfo.DeclaringType.AssemblyQualifiedName;
     }
     return info;
 }
示例#13
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);
        }
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            CodeFieldReferenceExpression newParent = (CodeFieldReferenceExpression)expression;

            if (newParent.TargetObject == null)
            {
                ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullFieldTarget, new object[] { newParent.FieldName }), 0x53d);
                item.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(item);
                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.NullFieldTarget, new object[] { newParent.FieldName }), 0x546);
                error2.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error2);
                return(null);
            }
            BindingFlags @public = BindingFlags.Public;

            if (newParent.TargetObject is CodeTypeReferenceExpression)
            {
                @public |= BindingFlags.FlattenHierarchy | BindingFlags.Static;
            }
            else
            {
                @public |= BindingFlags.Instance;
            }
            if (validation.AllowInternalMembers(expressionType))
            {
                @public |= BindingFlags.NonPublic;
            }
            FieldInfo field = expressionType.GetField(newParent.FieldName, @public);

            if (field == null)
            {
                ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownField, new object[] { newParent.FieldName, RuleDecompiler.DecompileType(expressionType) }), 0x54a);
                error3.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error3);
                return(null);
            }
            if (field.FieldType == null)
            {
                ValidationError error4 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, new object[] { newParent.FieldName }), 0x194);
                error4.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error4);
                return(null);
            }
            if (isWritten && field.IsLiteral)
            {
                ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.FieldSetNotAllowed, new object[] { newParent.FieldName, RuleDecompiler.DecompileType(expressionType) }), 0x17a);
                error5.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error5);
                return(null);
            }
            if (!validation.ValidateMemberAccess(newParent.TargetObject, expressionType, field, field.Name, newParent))
            {
                return(null);
            }
            validation.IsAuthorized(field.FieldType);
            return(new RuleFieldExpressionInfo(field));
        }
示例#15
0
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            string message;

            CodeFieldReferenceExpression fieldRefExpr = (CodeFieldReferenceExpression)expression;

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

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

            RuleExpressionInfo targetExprInfo = RuleExpressionWalker.Validate(validation, fieldRefExpr.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.NullFieldTarget, fieldRefExpr.FieldName);
                ValidationError error = new ValidationError(message, ErrorNumbers.Error_BindingTypeMissing);
                error.UserData[RuleUserDataKeys.ErrorObject] = fieldRefExpr;
                validation.Errors.Add(error);
                return null;
            }

            BindingFlags bindingFlags = BindingFlags.Public;
            if (fieldRefExpr.TargetObject is CodeTypeReferenceExpression)
                bindingFlags |= BindingFlags.Static | BindingFlags.FlattenHierarchy;
            else
                bindingFlags |= BindingFlags.Instance;
            if (validation.AllowInternalMembers(targetType))
                bindingFlags |= BindingFlags.NonPublic;

            FieldInfo fi = targetType.GetField(fieldRefExpr.FieldName, bindingFlags);
            if (fi == null)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.UnknownField, fieldRefExpr.FieldName, RuleDecompiler.DecompileType(targetType));
                ValidationError error = new ValidationError(message, ErrorNumbers.Error_CannotResolveMember);
                error.UserData[RuleUserDataKeys.ErrorObject] = fieldRefExpr;
                validation.Errors.Add(error);
                return null;
            }

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

            if (isWritten && fi.IsLiteral)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.FieldSetNotAllowed, fieldRefExpr.FieldName, RuleDecompiler.DecompileType(targetType));
                ValidationError error = new ValidationError(message, ErrorNumbers.Error_InvalidAssignTarget);
                error.UserData[RuleUserDataKeys.ErrorObject] = fieldRefExpr;
                validation.Errors.Add(error);
                return null;
            }

            if (!validation.ValidateMemberAccess(fieldRefExpr.TargetObject, targetType, fi, fi.Name, fieldRefExpr))
                return null;

            // Is it possible to set fi by validation.ResolveFieldOrProperty(targetType, fieldExpr.FieldName)?
            validation.IsAuthorized(fi.FieldType);
            return new RuleFieldExpressionInfo(fi);
        }
 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;
 }
 internal override bool Validate(RuleValidation validation, MemberInfo member, Type contextType, ParameterInfo[] parameters)
 {
     ValidationError error = null;
     if (string.IsNullOrEmpty(this.attributePath))
     {
         return true;
     }
     string[] strArray = this.attributePath.Split(new char[] { '/' });
     string str2 = strArray[0];
     int num = 0;
     if (this.attributeTarget == RuleAttributeTarget.This)
     {
         if (str2 == "this")
         {
             num++;
         }
     }
     else
     {
         bool flag2 = false;
         for (int j = 0; j < parameters.Length; j++)
         {
             ParameterInfo info = parameters[j];
             if (info.Name == str2)
             {
                 flag2 = true;
                 contextType = info.ParameterType;
                 break;
             }
         }
         if (!flag2)
         {
             error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleAttributeParameter, new object[] { str2, member.Name }), 420);
             error.UserData["ErrorObject"] = this;
             validation.AddError(error);
             return false;
         }
         num++;
     }
     int length = strArray.Length;
     string str3 = strArray[length - 1];
     if (string.IsNullOrEmpty(str3) || (str3 == "*"))
     {
         length--;
     }
     Type fieldType = contextType;
     for (int i = num; i < length; i++)
     {
         if (!(strArray[i] == "*"))
         {
             goto Label_0171;
         }
         error = new ValidationError(Messages.InvalidWildCardInPathQualifier, 0x195);
         error.UserData["ErrorObject"] = this;
         validation.AddError(error);
         return false;
     Label_0168:
         fieldType = fieldType.GetElementType();
     Label_0171:
         if (fieldType.IsArray)
         {
             goto Label_0168;
         }
         BindingFlags bindingAttr = BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance;
         if (validation.AllowInternalMembers(fieldType))
         {
             bindingAttr |= BindingFlags.NonPublic;
         }
         FieldInfo field = fieldType.GetField(strArray[i], bindingAttr);
         if (field != null)
         {
             fieldType = field.FieldType;
         }
         else
         {
             PropertyInfo property = fieldType.GetProperty(strArray[i], bindingAttr);
             if (property != null)
             {
                 fieldType = property.PropertyType;
             }
             else
             {
                 error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, new object[] { strArray[i] }), 390);
                 error.UserData["ErrorObject"] = this;
                 validation.AddError(error);
                 return false;
             }
         }
     }
     return true;
 }
示例#18
0
        public override bool Validate(RuleValidation validator)
        {
            if (validator == null)
            {
                throw new ArgumentNullException("validator");
            }

            bool success = true;

            if (path == null)
            {
                ValidationError error = new ValidationError(Messages.NullUpdate, ErrorNumbers.Error_ParameterNotSet);
                error.UserData[RuleUserDataKeys.ErrorObject] = this;
                validator.AddError(error);
                success = false;
            }

            // now make sure that the path is valid
            string[] parts = path.Split('/');
            if (parts[0] == "this")
            {
                Type currentType = validator.ThisType;
                for (int i = 1; i < parts.Length; ++i)
                {
                    if (parts[i] == "*")
                    {
                        if (i < parts.Length - 1)
                        {
                            // The "*" occurred in the middle of the path, which is a no-no.
                            ValidationError error = new ValidationError(Messages.InvalidWildCardInPathQualifier, ErrorNumbers.Error_InvalidWildCardInPathQualifier);
                            error.UserData[RuleUserDataKeys.ErrorObject] = this;
                            validator.AddError(error);
                            success = false;
                            break;
                        }
                        else
                        {
                            // It occurred at the end, which is okay.
                            break;
                        }
                    }
                    else if (string.IsNullOrEmpty(parts[i]) && i == parts.Length - 1)
                    {
                        // It's okay to end with a "/".
                        break;
                    }

                    while (currentType.IsArray)
                    {
                        currentType = currentType.GetElementType();
                    }

                    BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;
                    if (validator.AllowInternalMembers(currentType))
                    {
                        bindingFlags |= BindingFlags.NonPublic;
                    }
                    FieldInfo field = currentType.GetField(parts[i], bindingFlags);
                    if (field != null)
                    {
                        currentType = field.FieldType;
                    }
                    else
                    {
                        PropertyInfo property = currentType.GetProperty(parts[i], bindingFlags);
                        if (property != null)
                        {
                            currentType = property.PropertyType;
                        }
                        else
                        {
                            string          message = string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, parts[i]);
                            ValidationError error   = new ValidationError(message, ErrorNumbers.Error_InvalidUpdate);
                            error.UserData[RuleUserDataKeys.ErrorObject] = this;
                            validator.AddError(error);
                            success = false;
                            break;
                        }
                    }
                }
            }
            else
            {
                ValidationError error = new ValidationError(Messages.UpdateNotThis, ErrorNumbers.Error_InvalidUpdate);
                error.UserData[RuleUserDataKeys.ErrorObject] = this;
                validator.AddError(error);
                success = false;
            }

            return(success);
        }
        internal override bool Validate(RuleValidation validation, MemberInfo member, Type contextType, ParameterInfo[] parameters)
        {
            ValidationError error = null;
            string message = null;

            if (string.IsNullOrEmpty(attributePath))
            {
                // It is allowed to pass null or the empty string to [RuleRead] or [RuleWrite].  This
                // is how you indicate that a method or property has no dependencies or side effects.
                return true;
            }

            bool valid = true;

            string[] parts = attributePath.Split('/');

            // Check the first part.

            string firstPart = parts[0];
            int startOfRelativePortion = 0;
            if (attributeTarget == RuleAttributeTarget.This)
            {
                // When target is "This", the path is allowed to start with the token "this".  It is
                // then skipped for the rest of the validation, and the contextType remains what it
                // was when passed in.
                if (firstPart == "this")
                    ++startOfRelativePortion;
            }
            else
            {
                // When target is "Parameter", the path must start with the name of a parameter.
                bool found = false;
                for (int p = 0; p < parameters.Length; ++p)
                {
                    ParameterInfo param = parameters[p];
                    if (param.Name == firstPart)
                    {
                        found = true;

                        // The context type is the parameter type.
                        contextType = param.ParameterType;
                        break;
                    }
                }

                if (!found)
                {
                    message = string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleAttributeParameter, firstPart, member.Name);
                    error = new ValidationError(message, ErrorNumbers.Error_InvalidRuleAttributeParameter);
                    error.UserData[RuleUserDataKeys.ErrorObject] = this;
                    validation.AddError(error);
                    return false;
                }

                ++startOfRelativePortion;
            }

            int numParts = parts.Length;

            // Check the last part.  The last part is allowed to be empty, or "*".

            string lastPart = parts[numParts - 1];
            if (string.IsNullOrEmpty(lastPart) || lastPart == "*")
                numParts -= 1;

            // Check the rest of the parts.

            Type currentType = contextType;
            for (int i = startOfRelativePortion; i < numParts; ++i)
            {
                // Can't have embedded "*" wildcards.
                if (parts[i] == "*")
                {
                    // The "*" occurred in the middle of the path, which is a no-no.
                    error = new ValidationError(Messages.InvalidWildCardInPathQualifier, ErrorNumbers.Error_InvalidWildCardInPathQualifier);
                    error.UserData[RuleUserDataKeys.ErrorObject] = this;
                    validation.AddError(error);
                    valid = false;
                    break;
                }

                // Skip array types.
                while (currentType.IsArray)
                    currentType = currentType.GetElementType();

                // Make sure the member exists in the current type.
                BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;
                if (validation.AllowInternalMembers(currentType))
                    bindingFlags |= BindingFlags.NonPublic;

                FieldInfo field = currentType.GetField(parts[i], bindingFlags);
                if (field != null)
                {
                    currentType = field.FieldType;
                }
                else
                {
                    PropertyInfo property = currentType.GetProperty(parts[i], bindingFlags);
                    if (property != null)
                    {
                        currentType = property.PropertyType;
                    }
                    else
                    {
                        message = string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, parts[i]);
                        error = new ValidationError(message, ErrorNumbers.Error_UnknownFieldOrProperty);
                        error.UserData[RuleUserDataKeys.ErrorObject] = this;
                        validation.AddError(error);
                        valid = false;
                        break;
                    }
                }
            }

            return valid;
        }
        internal override bool Validate(RuleValidation validation, MemberInfo member, Type contextType, ParameterInfo[] parameters)
        {
            ValidationError error   = null;
            string          message = null;

            if (string.IsNullOrEmpty(attributePath))
            {
                // It is allowed to pass null or the empty string to [RuleRead] or [RuleWrite].  This
                // is how you indicate that a method or property has no dependencies or side effects.
                return(true);
            }

            bool valid = true;

            string[] parts = attributePath.Split('/');

            // Check the first part.

            string firstPart = parts[0];
            int    startOfRelativePortion = 0;

            if (attributeTarget == RuleAttributeTarget.This)
            {
                // When target is "This", the path is allowed to start with the token "this".  It is
                // then skipped for the rest of the validation, and the contextType remains what it
                // was when passed in.
                if (firstPart == "this")
                {
                    ++startOfRelativePortion;
                }
            }
            else
            {
                // When target is "Parameter", the path must start with the name of a parameter.
                bool found = false;
                for (int p = 0; p < parameters.Length; ++p)
                {
                    ParameterInfo param = parameters[p];
                    if (param.Name == firstPart)
                    {
                        found = true;

                        // The context type is the parameter type.
                        contextType = param.ParameterType;
                        break;
                    }
                }

                if (!found)
                {
                    message = string.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleAttributeParameter, firstPart, member.Name);
                    error   = new ValidationError(message, ErrorNumbers.Error_InvalidRuleAttributeParameter);
                    error.UserData[RuleUserDataKeys.ErrorObject] = this;
                    validation.AddError(error);
                    return(false);
                }

                ++startOfRelativePortion;
            }

            int numParts = parts.Length;

            // Check the last part.  The last part is allowed to be empty, or "*".

            string lastPart = parts[numParts - 1];

            if (string.IsNullOrEmpty(lastPart) || lastPart == "*")
            {
                numParts -= 1;
            }

            // Check the rest of the parts.

            Type currentType = contextType;

            for (int i = startOfRelativePortion; i < numParts; ++i)
            {
                // Can't have embedded "*" wildcards.
                if (parts[i] == "*")
                {
                    // The "*" occurred in the middle of the path, which is a no-no.
                    error = new ValidationError(Messages.InvalidWildCardInPathQualifier, ErrorNumbers.Error_InvalidWildCardInPathQualifier);
                    error.UserData[RuleUserDataKeys.ErrorObject] = this;
                    validation.AddError(error);
                    valid = false;
                    break;
                }

                // Skip array types.
                while (currentType.IsArray)
                {
                    currentType = currentType.GetElementType();
                }

                // Make sure the member exists in the current type.
                BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy;
                if (validation.AllowInternalMembers(currentType))
                {
                    bindingFlags |= BindingFlags.NonPublic;
                }

                FieldInfo field = currentType.GetField(parts[i], bindingFlags);
                if (field != null)
                {
                    currentType = field.FieldType;
                }
                else
                {
                    PropertyInfo property = currentType.GetProperty(parts[i], bindingFlags);
                    if (property != null)
                    {
                        currentType = property.PropertyType;
                    }
                    else
                    {
                        message = string.Format(CultureInfo.CurrentCulture, Messages.UpdateUnknownFieldOrProperty, parts[i]);
                        error   = new ValidationError(message, ErrorNumbers.Error_UnknownFieldOrProperty);
                        error.UserData[RuleUserDataKeys.ErrorObject] = this;
                        validation.AddError(error);
                        valid = false;
                        break;
                    }
                }
            }

            return(valid);
        }
示例#21
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)
 {
     CodeFieldReferenceExpression newParent = (CodeFieldReferenceExpression) expression;
     if (newParent.TargetObject == null)
     {
         ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullFieldTarget, new object[] { newParent.FieldName }), 0x53d);
         item.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(item);
         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.NullFieldTarget, new object[] { newParent.FieldName }), 0x546);
         error2.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error2);
         return null;
     }
     BindingFlags @public = BindingFlags.Public;
     if (newParent.TargetObject is CodeTypeReferenceExpression)
     {
         @public |= BindingFlags.FlattenHierarchy | BindingFlags.Static;
     }
     else
     {
         @public |= BindingFlags.Instance;
     }
     if (validation.AllowInternalMembers(expressionType))
     {
         @public |= BindingFlags.NonPublic;
     }
     FieldInfo field = expressionType.GetField(newParent.FieldName, @public);
     if (field == null)
     {
         ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownField, new object[] { newParent.FieldName, RuleDecompiler.DecompileType(expressionType) }), 0x54a);
         error3.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error3);
         return null;
     }
     if (field.FieldType == null)
     {
         ValidationError error4 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CouldNotDetermineMemberType, new object[] { newParent.FieldName }), 0x194);
         error4.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error4);
         return null;
     }
     if (isWritten && field.IsLiteral)
     {
         ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.FieldSetNotAllowed, new object[] { newParent.FieldName, RuleDecompiler.DecompileType(expressionType) }), 0x17a);
         error5.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error5);
         return null;
     }
     if (!validation.ValidateMemberAccess(newParent.TargetObject, expressionType, field, field.Name, newParent))
     {
         return null;
     }
     validation.IsAuthorized(field.FieldType);
     return new RuleFieldExpressionInfo(field);
 }
 internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
 {
     ValidationError error;
     CodeObjectCreateExpression newParent = (CodeObjectCreateExpression) expression;
     if (isWritten)
     {
         error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeObjectCreateExpression).ToString() }), 0x17a);
         error.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error);
         return null;
     }
     if (newParent.CreateType == null)
     {
         error = new ValidationError(Messages.NullTypeType, 0x53d);
         error.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error);
         return null;
     }
     Type type = validation.ResolveType(newParent.CreateType);
     if (type == null)
     {
         return null;
     }
     List<CodeExpression> argumentExprs = new List<CodeExpression>();
     try
     {
         if (!validation.PushParentExpression(newParent))
         {
             return null;
         }
         bool flag = false;
         for (int i = 0; i < newParent.Parameters.Count; i++)
         {
             CodeExpression expression3 = newParent.Parameters[i];
             if (expression3 == null)
             {
                 error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullConstructorParameter, new object[] { i.ToString(CultureInfo.CurrentCulture), RuleDecompiler.DecompileType(type) }), 0x53d);
                 error.UserData["ErrorObject"] = newParent;
                 validation.Errors.Add(error);
                 flag = true;
             }
             else
             {
                 if (RuleExpressionWalker.Validate(validation, expression3, false) == null)
                 {
                     flag = true;
                 }
                 argumentExprs.Add(expression3);
             }
         }
         if (flag)
         {
             return null;
         }
     }
     finally
     {
         validation.PopParentExpression();
     }
     BindingFlags constructorBindingFlags = BindingFlags.Public | BindingFlags.Instance;
     if (validation.AllowInternalMembers(type))
     {
         constructorBindingFlags |= BindingFlags.NonPublic;
     }
     if (type.IsValueType && (argumentExprs.Count == 0))
     {
         return new RuleExpressionInfo(type);
     }
     if (type.IsAbstract)
     {
         error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownConstructor, new object[] { RuleDecompiler.DecompileType(type) }), 0x137);
         error.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error);
         return null;
     }
     RuleConstructorExpressionInfo info2 = validation.ResolveConstructor(type, constructorBindingFlags, argumentExprs, out error);
     if (info2 == null)
     {
         error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.UnknownConstructor, new object[] { RuleDecompiler.DecompileType(type) }), 0x137);
         error.UserData["ErrorObject"] = newParent;
         validation.Errors.Add(error);
         return null;
     }
     return info2;
 }