Пример #1
0
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            CodeCastExpression childExpr   = (CodeCastExpression)expression;
            CodeExpression     expression3 = childExpr.Expression;

            if (expression3 == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(Messages.NullCastExpr);
                exception.Data["ErrorObject"] = childExpr;
                throw exception;
            }
            if (childExpr.TargetType == null)
            {
                RuleEvaluationException exception2 = new RuleEvaluationException(Messages.NullCastType);
                exception2.Data["ErrorObject"] = childExpr;
                throw exception2;
            }
            bool flag = RuleDecompiler.MustParenthesize(childExpr, parentExpression);

            if (flag)
            {
                stringBuilder.Append("(");
            }
            stringBuilder.Append("(");
            RuleDecompiler.DecompileType(stringBuilder, childExpr.TargetType);
            stringBuilder.Append(")");
            RuleExpressionWalker.Decompile(stringBuilder, expression3, childExpr);
            if (flag)
            {
                stringBuilder.Append(")");
            }
        }
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            CodeObjectCreateExpression childExpr = (CodeObjectCreateExpression)expression;
            bool flag = RuleDecompiler.MustParenthesize(childExpr, parentExpression);

            if (flag)
            {
                stringBuilder.Append("(");
            }
            stringBuilder.Append("new ");
            RuleDecompiler.DecompileType(stringBuilder, childExpr.CreateType);
            stringBuilder.Append('(');
            for (int i = 0; i < childExpr.Parameters.Count; i++)
            {
                CodeExpression expression3 = childExpr.Parameters[i];
                if (expression3 == null)
                {
                    RuleEvaluationException exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullConstructorTypeParameter, new object[] { i.ToString(CultureInfo.CurrentCulture), childExpr.CreateType }));
                    exception.Data["ErrorObject"] = childExpr;
                    throw exception;
                }
                if (i > 0)
                {
                    stringBuilder.Append(", ");
                }
                RuleExpressionWalker.Decompile(stringBuilder, expression3, null);
            }
            stringBuilder.Append(')');
            if (flag)
            {
                stringBuilder.Append(")");
            }
        }
Пример #3
0
        internal static object AdjustTypeWithCast(Type operandType, object operandValue, Type toType)
        {
            // if no conversion required, we are done
            if (operandType == toType)
            {
                return(operandValue);
            }

            object converted;

            if (AdjustValueStandard(operandType, operandValue, toType, out converted))
            {
                return(converted);
            }

            // handle enumerations (done above?)

            // now it's time for implicit and explicit user defined conversions
            ValidationError error;
            MethodInfo      conversion = RuleValidation.FindExplicitConversion(operandType, toType, out error);

            if (conversion == null)
            {
                if (error != null)
                {
                    throw new RuleEvaluationException(error.ErrorText);
                }

                throw new RuleEvaluationException(
                          string.Format(CultureInfo.CurrentCulture,
                                        Messages.CastIncompatibleTypes,
                                        RuleDecompiler.DecompileType(operandType),
                                        RuleDecompiler.DecompileType(toType)));
            }

            // now we have a method, need to do the conversion S -> Sx -> Tx -> T
            Type sx = conversion.GetParameters()[0].ParameterType;
            Type tx = conversion.ReturnType;

            object intermediateResult1;

            if (AdjustValueStandard(operandType, operandValue, sx, out intermediateResult1))
            {
                // we are happy with the first conversion, so call the user's static method
                object intermediateResult2 = conversion.Invoke(null, new object[] { intermediateResult1 });
                object intermediateResult3;
                if (AdjustValueStandard(tx, intermediateResult2, toType, out intermediateResult3))
                {
                    return(intermediateResult3);
                }
            }
            throw new RuleEvaluationException(
                      string.Format(CultureInfo.CurrentCulture,
                                    Messages.CastIncompatibleTypes,
                                    RuleDecompiler.DecompileType(operandType),
                                    RuleDecompiler.DecompileType(toType)));
        }
Пример #4
0
        public RuleExecution(RuleValidation validation, object thisObject)
        {
            if (validation == null)
            {
                throw new ArgumentNullException("validation");
            }
            if (thisObject == null)
            {
                throw new ArgumentNullException("thisObject");
            }
            if (validation.ThisType != thisObject.GetType())
            {
                throw new InvalidOperationException(
                          string.Format(CultureInfo.CurrentCulture, Messages.ValidationMismatch,
                                        RuleDecompiler.DecompileType(validation.ThisType),
                                        RuleDecompiler.DecompileType(thisObject.GetType())));
            }

            this.validation = validation;
            //this.activity = thisObject as Activity;
            this.thisObject        = thisObject;
            this.thisLiteralResult = new RuleLiteralResult(thisObject);
        }
        private static object EvaluateBinaryOperation(CodeBinaryOperatorExpression binaryExpr, Type lhsType, object lhsValue, CodeBinaryOperatorType operation, Type rhsType, object rhsValue)
        {
            Literal                 literal;
            Literal                 literal2;
            ArithmeticLiteral       literal3;
            ArithmeticLiteral       literal4;
            RuleEvaluationException exception;

            switch (operation)
            {
            case CodeBinaryOperatorType.Add:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.Add(literal4));

            case CodeBinaryOperatorType.Subtract:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.Subtract(literal4));

            case CodeBinaryOperatorType.Multiply:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.Multiply(literal4));

            case CodeBinaryOperatorType.Divide:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.Divide(literal4));

            case CodeBinaryOperatorType.Modulus:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.Modulus(literal4));

            case CodeBinaryOperatorType.IdentityInequality:
                return(lhsValue != rhsValue);

            case CodeBinaryOperatorType.IdentityEquality:
                return(lhsValue == rhsValue);

            case CodeBinaryOperatorType.ValueEquality:
                literal = Literal.MakeLiteral(lhsType, lhsValue);
                if (literal == null)
                {
                    break;
                }
                literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                if (literal2 == null)
                {
                    break;
                }
                return(literal.Equal(literal2));

            case CodeBinaryOperatorType.BitwiseOr:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.BitOr(literal4));

            case CodeBinaryOperatorType.BitwiseAnd:
                literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                if (literal3 == null)
                {
                    break;
                }
                literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                if (literal4 == null)
                {
                    break;
                }
                return(literal3.BitAnd(literal4));

            case CodeBinaryOperatorType.LessThan:
                literal = Literal.MakeLiteral(lhsType, lhsValue);
                if (literal == null)
                {
                    break;
                }
                literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                if (literal2 == null)
                {
                    break;
                }
                return(literal.LessThan(literal2));

            case CodeBinaryOperatorType.LessThanOrEqual:
                literal = Literal.MakeLiteral(lhsType, lhsValue);
                if (literal == null)
                {
                    break;
                }
                literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                if (literal2 == null)
                {
                    break;
                }
                return(literal.LessThanOrEqual(literal2));

            case CodeBinaryOperatorType.GreaterThan:
                literal = Literal.MakeLiteral(lhsType, lhsValue);
                if (literal == null)
                {
                    break;
                }
                literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                if (literal2 == null)
                {
                    break;
                }
                return(literal.GreaterThan(literal2));

            case CodeBinaryOperatorType.GreaterThanOrEqual:
                literal = Literal.MakeLiteral(lhsType, lhsValue);
                if (literal == null)
                {
                    break;
                }
                literal2 = Literal.MakeLiteral(rhsType, rhsValue);
                if (literal2 == null)
                {
                    break;
                }
                return(literal.GreaterThanOrEqual(literal2));

            default:
                exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { operation.ToString() }));
                exception.Data["ErrorObject"] = binaryExpr;
                throw exception;
            }
            exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpFails, new object[] { operation.ToString(), RuleDecompiler.DecompileType(lhsType), RuleDecompiler.DecompileType(rhsType) }));
            exception.Data["ErrorObject"] = binaryExpr;
            throw exception;
        }
        internal override bool Validate(RuleValidation validation)
        {
            bool               success = false;
            string             message;
            RuleExpressionInfo lhsExprInfo = null;

            if (assignStatement.Left == null)
            {
                ValidationError error = new ValidationError(Messages.NullAssignLeft, ErrorNumbers.Error_LeftOperandMissing);
                error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement;
                validation.Errors.Add(error);
            }
            else
            {
                lhsExprInfo = validation.ExpressionInfo(assignStatement.Left);
                if (lhsExprInfo == null)
                {
                    lhsExprInfo = RuleExpressionWalker.Validate(validation, assignStatement.Left, true);
                }
            }

            RuleExpressionInfo rhsExprInfo = null;

            if (assignStatement.Right == null)
            {
                ValidationError error = new ValidationError(Messages.NullAssignRight, ErrorNumbers.Error_RightOperandMissing);
                error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement;
                validation.Errors.Add(error);
            }
            else
            {
                rhsExprInfo = RuleExpressionWalker.Validate(validation, assignStatement.Right, false);
            }

            if (lhsExprInfo != null && rhsExprInfo != null)
            {
                Type expressionType = rhsExprInfo.ExpressionType;
                Type assignmentType = lhsExprInfo.ExpressionType;

                if (assignmentType == typeof(NullLiteral))
                {
                    // Can't assign to a null literal.
                    ValidationError error = new ValidationError(Messages.NullAssignLeft, ErrorNumbers.Error_LeftOperandInvalidType);
                    error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement;
                    validation.Errors.Add(error);
                    success = false;
                }
                else if (assignmentType == expressionType)
                {
                    // Easy case, they're both the same type.
                    success = true;
                }
                else
                {
                    // The types aren't the same, but it still might be a legal assignment.
                    if (!RuleValidation.TypesAreAssignable(expressionType, assignmentType, assignStatement.Right, out ValidationError error))
                    {
                        if (error == null)
                        {
                            message = string.Format(CultureInfo.CurrentCulture, Messages.AssignNotAllowed, RuleDecompiler.DecompileType(expressionType), RuleDecompiler.DecompileType(assignmentType));
                            error   = new ValidationError(message, ErrorNumbers.Error_OperandTypesIncompatible);
                        }
                        error.UserData[RuleUserDataKeys.ErrorObject] = assignStatement;
                        validation.Errors.Add(error);
                    }
                    else
                    {
                        success = true;
                    }
                }
            }

            return(success);
        }
        internal static RuleBinaryExpressionInfo ResultType(CodeBinaryOperatorType operation, Type lhs, CodeExpression lhsExpression, Type rhs, CodeExpression rhsExpression, RuleValidation validator, out ValidationError error)
        {
            TypeFlags flags;
            TypeFlags flags2;

            if (supportedTypes.TryGetValue(lhs, out flags) && supportedTypes.TryGetValue(rhs, out flags2))
            {
                Type resultType = ResultType(operation, flags, flags2);
                if (resultType != null)
                {
                    error = null;
                    return(new RuleBinaryExpressionInfo(lhs, rhs, resultType));
                }
                string errorText = string.Format(CultureInfo.CurrentCulture, Messages.ArithOpBadTypes, new object[] { operation.ToString(), (lhs == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(lhs), (rhs == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(rhs) });
                error = new ValidationError(errorText, 0x545);
                return(null);
            }
            MethodInfo mi = Literal.MapOperatorToMethod(operation, lhs, lhsExpression, rhs, rhsExpression, validator, out error);

            if (mi != null)
            {
                return(new RuleBinaryExpressionInfo(lhs, rhs, mi));
            }
            return(null);
        }
        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));
        }
Пример #9
0
        internal override bool Validate(RuleValidation validation)
        {
            bool flag = false;
            RuleExpressionInfo info = null;

            if (this.assignStatement.Left == null)
            {
                ValidationError item = new ValidationError(Messages.NullAssignLeft, 0x541);
                item.UserData["ErrorObject"] = this.assignStatement;
                validation.Errors.Add(item);
            }
            else
            {
                info = validation.ExpressionInfo(this.assignStatement.Left);
                if (info == null)
                {
                    info = RuleExpressionWalker.Validate(validation, this.assignStatement.Left, true);
                }
            }
            RuleExpressionInfo info2 = null;

            if (this.assignStatement.Right == null)
            {
                ValidationError error2 = new ValidationError(Messages.NullAssignRight, 0x543);
                error2.UserData["ErrorObject"] = this.assignStatement;
                validation.Errors.Add(error2);
            }
            else
            {
                info2 = RuleExpressionWalker.Validate(validation, this.assignStatement.Right, false);
            }
            if ((info == null) || (info2 == null))
            {
                return(flag);
            }
            Type expressionType = info2.ExpressionType;
            Type lhsType        = info.ExpressionType;

            if (lhsType == typeof(NullLiteral))
            {
                ValidationError error3 = new ValidationError(Messages.NullAssignLeft, 0x542);
                error3.UserData["ErrorObject"] = this.assignStatement;
                validation.Errors.Add(error3);
                return(false);
            }
            if (lhsType != expressionType)
            {
                ValidationError error = null;
                if (!RuleValidation.TypesAreAssignable(expressionType, lhsType, this.assignStatement.Right, out error))
                {
                    if (error == null)
                    {
                        error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.AssignNotAllowed, new object[] { RuleDecompiler.DecompileType(expressionType), RuleDecompiler.DecompileType(lhsType) }), 0x545);
                    }
                    error.UserData["ErrorObject"] = this.assignStatement;
                    validation.Errors.Add(error);
                    return(flag);
                }
            }
            return(true);
        }
Пример #10
0
        internal static object AdjustTypeWithCast(Type operandType, object operandValue, Type toType)
        {
            object          obj2;
            ValidationError error;
            object          obj3;

            if (operandType == toType)
            {
                return(operandValue);
            }
            if (AdjustValueStandard(operandType, operandValue, toType, out obj2))
            {
                return(obj2);
            }
            MethodInfo info = RuleValidation.FindExplicitConversion(operandType, toType, out error);

            if (info == null)
            {
                if (error != null)
                {
                    throw new RuleEvaluationException(error.ErrorText);
                }
                throw new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, new object[] { RuleDecompiler.DecompileType(operandType), RuleDecompiler.DecompileType(toType) }));
            }
            Type parameterType = info.GetParameters()[0].ParameterType;
            Type returnType    = info.ReturnType;

            if (AdjustValueStandard(operandType, operandValue, parameterType, out obj3))
            {
                object obj5;
                object obj4 = info.Invoke(null, new object[] { obj3 });
                if (AdjustValueStandard(returnType, obj4, toType, out obj5))
                {
                    return(obj5);
                }
            }
            throw new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, new object[] { RuleDecompiler.DecompileType(operandType), RuleDecompiler.DecompileType(toType) }));
        }
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            ValidationError            item           = null;
            Type                       expressionType = null;
            CodeArrayIndexerExpression newParent      = (CodeArrayIndexerExpression)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 info = RuleExpressionWalker.Validate(validation, newParent.TargetObject, false);
                if (info == null)
                {
                    return(null);
                }
                expressionType = info.ExpressionType;
                if (expressionType == null)
                {
                    return(null);
                }
                if (expressionType == typeof(NullLiteral))
                {
                    item = new ValidationError(Messages.NullIndexerTarget, 0x53d);
                    item.UserData["ErrorObject"] = newParent;
                    validation.Errors.Add(item);
                    return(null);
                }
                if (!expressionType.IsArray)
                {
                    item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotIndexType, new object[] { RuleDecompiler.DecompileType(expressionType) }), 0x19b);
                    item.UserData["ErrorObject"] = newParent;
                    validation.Errors.Add(item);
                    return(null);
                }
                int arrayRank = expressionType.GetArrayRank();
                if (newParent.Indices.Count != arrayRank)
                {
                    item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ArrayIndexBadRank, new object[] { arrayRank }), 0x19c);
                    item.UserData["ErrorObject"] = newParent;
                    validation.Errors.Add(item);
                    return(null);
                }
                bool flag = 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);
                        flag = true;
                        continue;
                    }
                    if (expression4 is CodeDirectionExpression)
                    {
                        item = new ValidationError(Messages.IndexerArgCannotBeRefOrOut, 0x19d);
                        item.UserData["ErrorObject"] = expression4;
                        validation.Errors.Add(item);
                        flag = 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);
                        flag = true;
                    }
                    RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, expression4, false);
                    if (info2 != null)
                    {
                        Type type = info2.ExpressionType;
                        switch (Type.GetTypeCode(type))
                        {
                        case TypeCode.Char:
                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.Int64:
                        {
                            continue;
                        }
                        }
                        item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ArrayIndexBadType, new object[] { RuleDecompiler.DecompileType(type) }), 0x19e);
                        item.UserData["ErrorObject"] = expression4;
                        validation.Errors.Add(item);
                        flag = true;
                        continue;
                    }
                    flag = true;
                }
                if (flag)
                {
                    return(null);
                }
            }
            finally
            {
                validation.PopParentExpression();
            }
            return(new RuleExpressionInfo(expressionType.GetElementType()));
        }
        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);
        }
Пример #13
0
        private static bool AdjustValueStandard(Type operandType, object operandValue, Type toType, out object converted)
        {
            // assume it's the same for now
            converted = operandValue;

            // check for null
            if (operandValue == null)
            {
                // are we converting to a value type?
                if (toType.IsValueType)
                {
                    // is the conversion to nullable?
                    if (!ConditionHelper.IsNullableValueType(toType))
                    {
                        // value type and null, so no conversion possible
                        string message = string.Format(CultureInfo.CurrentCulture, Messages.CannotCastNullToValueType, RuleDecompiler.DecompileType(toType));
                        throw new InvalidCastException(message);
                    }

                    // here we have a Nullable<T>
                    // however, we may need to call the implicit conversion operator if the types are not compatible
                    converted = Activator.CreateInstance(toType);
                    return(RuleValidation.StandardImplicitConversion(operandType, toType, null, out ValidationError error));
                }

                // not a value type, so null is valid
                return(true);
            }

            // check simple cases
            Type currentType = operandValue.GetType();

            if (currentType == toType)
            {
                return(true);
            }

            // now the fun begins
            // this should handle most class conversions
            if (toType.IsAssignableFrom(currentType))
            {
                return(true);
            }

            // handle the numerics (both implicit and explicit), along with nullable
            // note that if the value was null, it's already handled, so value cannot be nullable
            if ((currentType.IsValueType) && (toType.IsValueType))
            {
                if (currentType.IsEnum)
                {
                    // strip off the enum representation
                    currentType = Enum.GetUnderlyingType(currentType);
                    ArithmeticLiteral literal = ArithmeticLiteral.MakeLiteral(currentType, operandValue);
                    operandValue = literal.Value;
                }

                bool resultNullable = ConditionHelper.IsNullableValueType(toType);
                Type resultType     = (resultNullable) ? Nullable.GetUnderlyingType(toType) : toType;

                if (resultType.IsEnum)
                {
                    // Enum.ToObject may throw if currentType is not type SByte,
                    // Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64.
                    // So we adjust currentValue to the underlying type (which may throw if out of range)
                    Type underlyingType = Enum.GetUnderlyingType(resultType);
                    if (AdjustValueStandard(currentType, operandValue, underlyingType, out object adjusted))
                    {
                        converted = Enum.ToObject(resultType, adjusted);
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                }
                else if ((resultType.IsPrimitive) || (resultType == typeof(decimal)))
                {
                    // resultType must be a primitive to continue (not a struct)
                    // (enums and generics handled above)
                    if (currentType == typeof(char))
                    {
                        char c = (char)operandValue;
                        if (resultType == typeof(float))
                        {
                            converted = (float)c;
                        }
                        else if (resultType == typeof(double))
                        {
                            converted = (double)c;
                        }
                        else if (resultType == typeof(decimal))
                        {
                            converted = (decimal)c;
                        }
                        else
                        {
                            converted = ((IConvertible)c).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else if (currentType == typeof(float))
                    {
                        float f = (float)operandValue;
                        if (resultType == typeof(char))
                        {
                            converted = (char)f;
                        }
                        else
                        {
                            converted = ((IConvertible)f).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else if (currentType == typeof(double))
                    {
                        double d = (double)operandValue;
                        if (resultType == typeof(char))
                        {
                            converted = (char)d;
                        }
                        else
                        {
                            converted = ((IConvertible)d).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else if (currentType == typeof(decimal))
                    {
                        decimal d = (decimal)operandValue;
                        if (resultType == typeof(char))
                        {
                            converted = (char)d;
                        }
                        else
                        {
                            converted = ((IConvertible)d).ToType(resultType, CultureInfo.CurrentCulture);
                        }
                        if (resultNullable)
                        {
                            converted = Activator.CreateInstance(toType, converted);
                        }
                        return(true);
                    }
                    else
                    {
                        if (operandValue is IConvertible convert)
                        {
                            try
                            {
                                converted = convert.ToType(resultType, CultureInfo.CurrentCulture);
                                if (resultNullable)
                                {
                                    converted = Activator.CreateInstance(toType, converted);
                                }
                                return(true);
                            }
                            catch (InvalidCastException)
                            {
                                // not IConvertable, so can't do it
                                return(false);
                            }
                        }
                    }
                }
            }

            // no luck with standard conversions, so no conversion done
            return(false);
        }
Пример #14
0
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            object obj3;
            CodeMethodInvokeExpression expression2 = (CodeMethodInvokeExpression)expression;
            object obj2 = RuleExpressionWalker.Evaluate(execution, expression2.Method.TargetObject).Value;
            RuleMethodInvokeExpressionInfo info = execution.Validation.ExpressionInfo(expression2) as RuleMethodInvokeExpressionInfo;

            if (info == null)
            {
                InvalidOperationException exception = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
                exception.Data["ErrorObject"] = expression2;
                throw exception;
            }
            MethodInfo methodInfo = info.MethodInfo;

            if (!methodInfo.IsStatic && (obj2 == null))
            {
                RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullMethod, new object[] { expression2.Method.MethodName }));
                exception2.Data["ErrorObject"] = expression2;
                throw exception2;
            }
            object[] parameters = null;
            RuleExpressionResult[] resultArray = null;
            if ((expression2.Parameters != null) && (expression2.Parameters.Count > 0))
            {
                int             count     = expression2.Parameters.Count;
                ParameterInfo[] infoArray = methodInfo.GetParameters();
                parameters = new object[infoArray.Length];
                int length = infoArray.Length;
                if (info.NeedsParamsExpansion)
                {
                    length--;
                }
                int index = 0;
                while (index < length)
                {
                    Type expressionType                 = execution.Validation.ExpressionInfo(expression2.Parameters[index]).ExpressionType;
                    RuleExpressionResult    result      = RuleExpressionWalker.Evaluate(execution, expression2.Parameters[index]);
                    CodeDirectionExpression expression3 = expression2.Parameters[index] as CodeDirectionExpression;
                    if ((expression3 != null) && ((expression3.Direction == FieldDirection.Ref) || (expression3.Direction == FieldDirection.Out)))
                    {
                        if (resultArray == null)
                        {
                            resultArray = new RuleExpressionResult[expression2.Parameters.Count];
                        }
                        resultArray[index] = result;
                        if (expression3.Direction != FieldDirection.Out)
                        {
                            parameters[index] = Executor.AdjustType(expressionType, result.Value, infoArray[index].ParameterType);
                        }
                    }
                    else
                    {
                        parameters[index] = Executor.AdjustType(expressionType, result.Value, infoArray[index].ParameterType);
                    }
                    index++;
                }
                if (length < count)
                {
                    ParameterInfo info3         = infoArray[length];
                    Type          parameterType = info3.ParameterType;
                    Type          elementType   = parameterType.GetElementType();
                    Array         array         = (Array)parameterType.InvokeMember(parameterType.Name, BindingFlags.CreateInstance, null, null, new object[] { count - index }, CultureInfo.CurrentCulture);
                    while (index < count)
                    {
                        Type operandType             = execution.Validation.ExpressionInfo(expression2.Parameters[index]).ExpressionType;
                        RuleExpressionResult result2 = RuleExpressionWalker.Evaluate(execution, expression2.Parameters[index]);
                        array.SetValue(Executor.AdjustType(operandType, result2.Value, elementType), (int)(index - length));
                        index++;
                    }
                    parameters[length] = array;
                }
            }
            try
            {
                obj3 = methodInfo.Invoke(obj2, parameters);
            }
            catch (TargetInvocationException exception3)
            {
                if (exception3.InnerException == null)
                {
                    throw;
                }
                throw new TargetInvocationException(string.Format(CultureInfo.CurrentCulture, Messages.Error_MethodInvoke, new object[] { RuleDecompiler.DecompileType(methodInfo.ReflectedType), methodInfo.Name, exception3.InnerException.Message }), exception3.InnerException);
            }
            if (resultArray != null)
            {
                for (int i = 0; i < expression2.Parameters.Count; i++)
                {
                    if (resultArray[i] != null)
                    {
                        resultArray[i].Value = parameters[i];
                    }
                }
            }
            return(new RuleLiteralResult(obj3));
        }
Пример #15
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));
        }
Пример #16
0
        private static bool AdjustValueStandard(Type operandType, object operandValue, Type toType, out object converted)
        {
            converted = operandValue;
            if (operandValue == null)
            {
                ValidationError error;
                if (!toType.IsValueType)
                {
                    return(true);
                }
                if (!ConditionHelper.IsNullableValueType(toType))
                {
                    throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, Messages.CannotCastNullToValueType, new object[] { RuleDecompiler.DecompileType(toType) }));
                }
                converted = Activator.CreateInstance(toType);
                return(RuleValidation.StandardImplicitConversion(operandType, toType, null, out error));
            }
            Type c = operandValue.GetType();

            if (c == toType)
            {
                return(true);
            }
            if (toType.IsAssignableFrom(c))
            {
                return(true);
            }
            if (c.IsValueType && toType.IsValueType)
            {
                if (c.IsEnum)
                {
                    c            = Enum.GetUnderlyingType(c);
                    operandValue = ArithmeticLiteral.MakeLiteral(c, operandValue).Value;
                }
                bool flag     = ConditionHelper.IsNullableValueType(toType);
                Type enumType = flag ? Nullable.GetUnderlyingType(toType) : toType;
                if (enumType.IsEnum)
                {
                    object obj2;
                    Type   underlyingType = Enum.GetUnderlyingType(enumType);
                    if (AdjustValueStandard(c, operandValue, underlyingType, out obj2))
                    {
                        converted = Enum.ToObject(enumType, obj2);
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                }
                else if (enumType.IsPrimitive || (enumType == typeof(decimal)))
                {
                    if (c == typeof(char))
                    {
                        char ch = (char)operandValue;
                        if (enumType == typeof(float))
                        {
                            converted = (float)ch;
                        }
                        else if (enumType == typeof(double))
                        {
                            converted = (double)ch;
                        }
                        else if (enumType == typeof(decimal))
                        {
                            converted = ch;
                        }
                        else
                        {
                            converted = ((IConvertible)ch).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    if (c == typeof(float))
                    {
                        float num = (float)operandValue;
                        if (enumType == typeof(char))
                        {
                            converted = (char)((ushort)num);
                        }
                        else
                        {
                            converted = ((IConvertible)num).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    if (c == typeof(double))
                    {
                        double num2 = (double)operandValue;
                        if (enumType == typeof(char))
                        {
                            converted = (char)((ushort)num2);
                        }
                        else
                        {
                            converted = ((IConvertible)num2).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    if (c == typeof(decimal))
                    {
                        decimal num3 = (decimal)operandValue;
                        if (enumType == typeof(char))
                        {
                            converted = (char)num3;
                        }
                        else
                        {
                            converted = ((IConvertible)num3).ToType(enumType, CultureInfo.CurrentCulture);
                        }
                        if (flag)
                        {
                            converted = Activator.CreateInstance(toType, new object[] { converted });
                        }
                        return(true);
                    }
                    IConvertible convertible = operandValue as IConvertible;
                    if (convertible != null)
                    {
                        try
                        {
                            converted = convertible.ToType(enumType, CultureInfo.CurrentCulture);
                            if (flag)
                            {
                                converted = Activator.CreateInstance(toType, new object[] { converted });
                            }
                            return(true);
                        }
                        catch (InvalidCastException)
                        {
                            return(false);
                        }
                    }
                }
            }
            return(false);
        }
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            ValidationError error;
            CodeBinaryOperatorExpression newParent = (CodeBinaryOperatorExpression)expression;

            if (!validation.PushParentExpression(newParent))
            {
                return(null);
            }
            if (isWritten)
            {
                error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeBinaryOperatorExpression).ToString() }), 0x17a);
                error.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error);
            }
            RuleExpressionInfo info  = null;
            RuleExpressionInfo info2 = null;

            if (newParent.Left == null)
            {
                error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpLHS, new object[] { newParent.Operator.ToString() }), 0x541);
                error.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error);
            }
            else
            {
                if (newParent.Left is CodeTypeReferenceExpression)
                {
                    error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { newParent.Left.GetType().FullName }), 0x548);
                    error.UserData["ErrorObject"] = newParent.Left;
                    validation.AddError(error);
                    return(null);
                }
                info = RuleExpressionWalker.Validate(validation, newParent.Left, false);
            }
            if (newParent.Right == null)
            {
                error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpRHS, new object[] { newParent.Operator.ToString() }), 0x543);
                error.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error);
            }
            else
            {
                if (newParent.Right is CodeTypeReferenceExpression)
                {
                    error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { newParent.Right.GetType().FullName }), 0x548);
                    error.UserData["ErrorObject"] = newParent.Right;
                    validation.AddError(error);
                    return(null);
                }
                info2 = RuleExpressionWalker.Validate(validation, newParent.Right, false);
            }
            validation.PopParentExpression();
            RuleBinaryExpressionInfo info3 = null;

            if ((info != null) && (info2 != null))
            {
                Type expressionType = info.ExpressionType;
                Type rhs            = info2.ExpressionType;
                switch (newParent.Operator)
                {
                case CodeBinaryOperatorType.Add:
                case CodeBinaryOperatorType.Subtract:
                case CodeBinaryOperatorType.Multiply:
                case CodeBinaryOperatorType.Divide:
                case CodeBinaryOperatorType.Modulus:
                case CodeBinaryOperatorType.BitwiseOr:
                case CodeBinaryOperatorType.BitwiseAnd:
                    info3 = ArithmeticLiteral.ResultType(newParent.Operator, expressionType, newParent.Left, rhs, newParent.Right, validation, out error);
                    if (info3 == null)
                    {
                        if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left)))
                        {
                            error.UserData["ErrorObject"] = newParent;
                            validation.Errors.Add(error);
                        }
                        else
                        {
                            info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(ulong));
                        }
                    }
                    goto Label_063E;

                case CodeBinaryOperatorType.IdentityInequality:
                case CodeBinaryOperatorType.IdentityEquality:
                    info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool));
                    goto Label_063E;

                case CodeBinaryOperatorType.ValueEquality:
                    info3 = Literal.AllowedComparison(expressionType, newParent.Left, rhs, newParent.Right, newParent.Operator, validation, out error);
                    if (info3 == null)
                    {
                        if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left)))
                        {
                            error.UserData["ErrorObject"] = newParent;
                            validation.Errors.Add(error);
                        }
                        else
                        {
                            info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool));
                        }
                    }
                    goto Label_063E;

                case CodeBinaryOperatorType.BooleanOr:
                case CodeBinaryOperatorType.BooleanAnd:
                    info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool));
                    if (expressionType != typeof(bool))
                    {
                        error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.LogicalOpBadTypeLHS, new object[] { newParent.Operator.ToString(), (expressionType == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(expressionType) }), 0x542);
                        error.UserData["ErrorObject"] = newParent;
                        validation.Errors.Add(error);
                        info3 = null;
                    }
                    if (rhs != typeof(bool))
                    {
                        error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.LogicalOpBadTypeRHS, new object[] { newParent.Operator.ToString(), (rhs == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(rhs) }), 0x544);
                        error.UserData["ErrorObject"] = newParent;
                        validation.Errors.Add(error);
                        info3 = null;
                    }
                    goto Label_063E;

                case CodeBinaryOperatorType.LessThan:
                case CodeBinaryOperatorType.LessThanOrEqual:
                case CodeBinaryOperatorType.GreaterThan:
                case CodeBinaryOperatorType.GreaterThanOrEqual:
                    info3 = Literal.AllowedComparison(expressionType, newParent.Left, rhs, newParent.Right, newParent.Operator, validation, out error);
                    if (info3 == null)
                    {
                        if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left)))
                        {
                            error.UserData["ErrorObject"] = newParent;
                            validation.Errors.Add(error);
                        }
                        else
                        {
                            info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool));
                        }
                    }
                    goto Label_063E;
                }
                error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { newParent.Operator.ToString() }), 0x548);
                error.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error);
            }
Label_063E:
            if (info3 != null)
            {
                MethodInfo methodInfo = info3.MethodInfo;
                if (methodInfo == null)
                {
                    return(info3);
                }
                object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuleAttribute), true);
                if ((customAttributes == null) || (customAttributes.Length <= 0))
                {
                    return(info3);
                }
                Stack <MemberInfo> stack = new Stack <MemberInfo>();
                stack.Push(methodInfo);
                bool flag = true;
                foreach (RuleAttribute attribute in customAttributes)
                {
                    if (!attribute.Validate(validation, methodInfo, methodInfo.DeclaringType, methodInfo.GetParameters()))
                    {
                        flag = false;
                    }
                }
                stack.Pop();
                if (!flag)
                {
                    return(null);
                }
            }
            return(info3);
        }
Пример #18
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);
        }
Пример #19
0
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            CodeCastExpression expression2 = (CodeCastExpression)expression;

            if (isWritten)
            {
                ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeCastExpression).ToString() }), 0x17a);
                item.UserData["ErrorObject"] = expression2;
                validation.Errors.Add(item);
                return(null);
            }
            if (expression2.Expression == null)
            {
                ValidationError error2 = new ValidationError(Messages.NullCastExpr, 0x53d);
                error2.UserData["ErrorObject"] = expression2;
                validation.Errors.Add(error2);
                return(null);
            }
            if (expression2.Expression is CodeTypeReferenceExpression)
            {
                ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression2.Expression.GetType().FullName }), 0x548);
                error.UserData["ErrorObject"] = expression2.Expression;
                validation.AddError(error);
                return(null);
            }
            if (expression2.TargetType == null)
            {
                ValidationError error4 = new ValidationError(Messages.NullCastType, 0x53d);
                error4.UserData["ErrorObject"] = expression2;
                validation.Errors.Add(error4);
                return(null);
            }
            RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, expression2.Expression, false);

            if (info == null)
            {
                return(null);
            }
            Type expressionType = info.ExpressionType;
            Type type           = validation.ResolveType(expression2.TargetType);

            if (type == null)
            {
                return(null);
            }
            if (expressionType == typeof(NullLiteral))
            {
                if (ConditionHelper.IsNonNullableValueType(type))
                {
                    ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CastOfNullInvalid, new object[] { RuleDecompiler.DecompileType(type) }), 0x53d);
                    error5.UserData["ErrorObject"] = expression2;
                    validation.Errors.Add(error5);
                    return(null);
                }
            }
            else
            {
                Type type3 = expressionType;
                if (ConditionHelper.IsNullableValueType(type3))
                {
                    type3 = type3.GetGenericArguments()[0];
                }
                Type type4 = type;
                if (ConditionHelper.IsNullableValueType(type4))
                {
                    type4 = type4.GetGenericArguments()[0];
                }
                bool flag = false;
                if (type3.IsValueType && type4.IsValueType)
                {
                    if (type3.IsEnum)
                    {
                        flag = type4.IsEnum || IsNumeric(type4);
                    }
                    else if (type4.IsEnum)
                    {
                        flag = IsNumeric(type3);
                    }
                    else if (type3 == typeof(char))
                    {
                        flag = IsNumeric(type4);
                    }
                    else if (type4 == typeof(char))
                    {
                        flag = IsNumeric(type3);
                    }
                    else if (type3.IsPrimitive && type4.IsPrimitive)
                    {
                        try
                        {
                            Convert.ChangeType(Activator.CreateInstance(type3), type4, CultureInfo.CurrentCulture);
                            flag = true;
                        }
                        catch (Exception)
                        {
                            flag = false;
                        }
                    }
                }
                if (!flag)
                {
                    ValidationError error6;
                    flag = RuleValidation.ExplicitConversionSpecified(expressionType, type, out error6);
                    if (error6 != null)
                    {
                        error6.UserData["ErrorObject"] = expression2;
                        validation.Errors.Add(error6);
                        return(null);
                    }
                }
                if (!flag)
                {
                    ValidationError error7 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, new object[] { RuleDecompiler.DecompileType(expressionType), RuleDecompiler.DecompileType(type) }), 0x53d);
                    error7.UserData["ErrorObject"] = expression2;
                    validation.Errors.Add(error7);
                    return(null);
                }
            }
            return(new RuleExpressionInfo(type));
        }
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            object obj2;
            CodeObjectCreateExpression expression2 = (CodeObjectCreateExpression)expression;

            if (expression2.CreateType == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(Messages.NullTypeType);
                exception.Data["ErrorObject"] = expression2;
                throw exception;
            }
            RuleExpressionInfo info = execution.Validation.ExpressionInfo(expression2);

            if (info == null)
            {
                InvalidOperationException exception2 = new InvalidOperationException(Messages.ExpressionNotValidated);
                exception2.Data["ErrorObject"] = expression2;
                throw exception2;
            }
            RuleConstructorExpressionInfo info2 = info as RuleConstructorExpressionInfo;

            if (info2 == null)
            {
                return(new RuleLiteralResult(Activator.CreateInstance(info.ExpressionType)));
            }
            ConstructorInfo constructorInfo = info2.ConstructorInfo;

            object[] parameters = null;
            RuleExpressionResult[] resultArray = null;
            if ((expression2.Parameters != null) && (expression2.Parameters.Count > 0))
            {
                int             count     = expression2.Parameters.Count;
                ParameterInfo[] infoArray = constructorInfo.GetParameters();
                parameters = new object[infoArray.Length];
                int length = infoArray.Length;
                if (info2.NeedsParamsExpansion)
                {
                    length--;
                }
                int index = 0;
                while (index < length)
                {
                    Type expressionType                 = execution.Validation.ExpressionInfo(expression2.Parameters[index]).ExpressionType;
                    RuleExpressionResult    result      = RuleExpressionWalker.Evaluate(execution, expression2.Parameters[index]);
                    CodeDirectionExpression expression3 = expression2.Parameters[index] as CodeDirectionExpression;
                    if ((expression3 != null) && ((expression3.Direction == FieldDirection.Ref) || (expression3.Direction == FieldDirection.Out)))
                    {
                        if (resultArray == null)
                        {
                            resultArray = new RuleExpressionResult[count];
                        }
                        resultArray[index] = result;
                    }
                    parameters[index] = Executor.AdjustType(expressionType, result.Value, infoArray[index].ParameterType);
                    index++;
                }
                if (length < count)
                {
                    ParameterInfo info4       = infoArray[length];
                    Type          elementType = info4.ParameterType.GetElementType();
                    Array         array       = Array.CreateInstance(elementType, (int)(count - index));
                    while (index < count)
                    {
                        Type operandType             = execution.Validation.ExpressionInfo(expression2.Parameters[index]).ExpressionType;
                        RuleExpressionResult result2 = RuleExpressionWalker.Evaluate(execution, expression2.Parameters[index]);
                        array.SetValue(Executor.AdjustType(operandType, result2.Value, elementType), (int)(index - length));
                        index++;
                    }
                    parameters[length] = array;
                }
            }
            try
            {
                obj2 = constructorInfo.Invoke(parameters);
            }
            catch (TargetInvocationException exception3)
            {
                if (exception3.InnerException == null)
                {
                    throw;
                }
                throw new TargetInvocationException(string.Format(CultureInfo.CurrentCulture, Messages.Error_ConstructorInvoke, new object[] { RuleDecompiler.DecompileType(info2.ExpressionType), exception3.InnerException.Message }), exception3.InnerException);
            }
            if (resultArray != null)
            {
                for (int i = 0; i < expression2.Parameters.Count; i++)
                {
                    if (resultArray[i] != null)
                    {
                        resultArray[i].Value = parameters[i];
                    }
                }
            }
            return(new RuleLiteralResult(obj2));
        }
Пример #21
0
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            CodeCastExpression expression2  = (CodeCastExpression)expression;
            object             operandValue = RuleExpressionWalker.Evaluate(execution, expression2.Expression).Value;
            RuleExpressionInfo info         = execution.Validation.ExpressionInfo(expression2);

            if (info == null)
            {
                InvalidOperationException exception = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
                exception.Data["ErrorObject"] = expression2;
                throw exception;
            }
            Type expressionType = info.ExpressionType;

            if (operandValue == null)
            {
                if (ConditionHelper.IsNonNullableValueType(expressionType))
                {
                    RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, new object[] { Messages.NullValue, RuleDecompiler.DecompileType(expressionType) }));
                    exception2.Data["ErrorObject"] = expression2;
                    throw exception2;
                }
            }
            else
            {
                operandValue = Executor.AdjustTypeWithCast(execution.Validation.ExpressionInfo(expression2.Expression).ExpressionType, operandValue, expressionType);
            }
            return(new RuleLiteralResult(operandValue));
        }
Пример #22
0
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            CodeTypeReferenceExpression expression2 = (CodeTypeReferenceExpression)expression;

            RuleDecompiler.DecompileType(stringBuilder, expression2.Type);
        }