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(")");
            }
        }
 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)
 {
     CodeIndexerExpression expression2 = (CodeIndexerExpression) expression;
     CodeExpression targetObject = expression2.TargetObject;
     if (targetObject == null)
     {
         RuleEvaluationException exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullIndexerTarget, new object[0]));
         exception.Data["ErrorObject"] = expression2;
         throw exception;
     }
     if ((expression2.Indices == null) || (expression2.Indices.Count == 0))
     {
         RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.MissingIndexExpressions, new object[0]));
         exception2.Data["ErrorObject"] = expression2;
         throw exception2;
     }
     RuleExpressionWalker.Decompile(stringBuilder, targetObject, expression2);
     stringBuilder.Append('[');
     RuleExpressionWalker.Decompile(stringBuilder, expression2.Indices[0], null);
     for (int i = 1; i < expression2.Indices.Count; i++)
     {
         stringBuilder.Append(", ");
         RuleExpressionWalker.Decompile(stringBuilder, expression2.Indices[i], null);
     }
     stringBuilder.Append(']');
 }
 internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
 {
     CodeMethodInvokeExpression expression2 = (CodeMethodInvokeExpression) expression;
     if ((expression2.Method == null) || (expression2.Method.TargetObject == null))
     {
         RuleEvaluationException exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, new object[] { expression2.Method.MethodName }));
         exception.Data["ErrorObject"] = expression2;
         throw exception;
     }
     CodeExpression targetObject = expression2.Method.TargetObject;
     RuleExpressionWalker.Decompile(stringBuilder, targetObject, expression2);
     stringBuilder.Append('.');
     stringBuilder.Append(expression2.Method.MethodName);
     stringBuilder.Append('(');
     if (expression2.Parameters != null)
     {
         for (int i = 0; i < expression2.Parameters.Count; i++)
         {
             CodeExpression expression4 = expression2.Parameters[i];
             if (expression4 == null)
             {
                 RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTypeParameter, new object[] { i.ToString(CultureInfo.CurrentCulture), expression2.Method.MethodName }));
                 exception2.Data["ErrorObject"] = expression2;
                 throw exception2;
             }
             if (i > 0)
             {
                 stringBuilder.Append(", ");
             }
             RuleExpressionWalker.Decompile(stringBuilder, expression4, null);
         }
     }
     stringBuilder.Append(')');
 }
 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);
 }
Пример #6
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));
        }
Пример #7
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)
        {
            CodeArrayIndexerExpression expression2  = (CodeArrayIndexerExpression)expression;
            CodeExpression             targetObject = expression2.TargetObject;

            if (targetObject == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullIndexerTarget, new object[0]));
                exception.Data["ErrorObject"] = expression2;
                throw exception;
            }
            if ((expression2.Indices == null) || (expression2.Indices.Count == 0))
            {
                RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.MissingIndexExpressions, new object[0]));
                exception2.Data["ErrorObject"] = expression2;
                throw exception2;
            }
            RuleExpressionWalker.Decompile(stringBuilder, targetObject, expression2);
            stringBuilder.Append('[');
            RuleExpressionWalker.Decompile(stringBuilder, expression2.Indices[0], null);
            for (int i = 1; i < expression2.Indices.Count; i++)
            {
                stringBuilder.Append(", ");
                RuleExpressionWalker.Decompile(stringBuilder, expression2.Indices[i], null);
            }
            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(")");
     }
 }
Пример #10
0
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            CodeIndexerExpression      expression2 = (CodeIndexerExpression)expression;
            RulePropertyExpressionInfo info        = execution.Validation.ExpressionInfo(expression2) as RulePropertyExpressionInfo;

            if (info == null)
            {
                InvalidOperationException exception = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
                exception.Data["ErrorObject"] = expression2;
                throw exception;
            }
            PropertyInfo propertyInfo = info.PropertyInfo;
            object       targetObject = RuleExpressionWalker.Evaluate(execution, expression2.TargetObject).Value;

            if (targetObject == null)
            {
                RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullIndexer, new object[0]));
                exception2.Data["ErrorObject"] = expression2;
                throw exception2;
            }
            int count = expression2.Indices.Count;

            ParameterInfo[] indexParameters  = propertyInfo.GetIndexParameters();
            object[]        indexerArguments = new object[indexParameters.Length];
            int             length           = indexParameters.Length;

            if (info.NeedsParamsExpansion)
            {
                length--;
            }
            int index = 0;

            while (index < length)
            {
                Type expressionType         = execution.Validation.ExpressionInfo(expression2.Indices[index]).ExpressionType;
                RuleExpressionResult result = RuleExpressionWalker.Evaluate(execution, expression2.Indices[index]);
                indexerArguments[index] = Executor.AdjustType(expressionType, result.Value, indexParameters[index].ParameterType);
                index++;
            }
            if (length < count)
            {
                ParameterInfo info3         = indexParameters[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.Indices[index]).ExpressionType;
                    RuleExpressionResult result2 = RuleExpressionWalker.Evaluate(execution, expression2.Indices[index]);
                    array.SetValue(Executor.AdjustType(operandType, result2.Value, elementType), (int)(index - length));
                    index++;
                }
                indexerArguments[length] = array;
            }
            return(new RulePropertyResult(propertyInfo, targetObject, indexerArguments));
        }
 internal override void Decompile(StringBuilder decompilation)
 {
     if (this.exprStatement.Expression == null)
     {
         RuleEvaluationException exception = new RuleEvaluationException(Messages.InvokeStatementNull);
         exception.Data["ErrorObject"] = this.exprStatement;
         throw exception;
     }
     RuleExpressionWalker.Decompile(decompilation, this.exprStatement.Expression, null);
 }
Пример #12
0
        internal override void Decompile(StringBuilder decompilation)
        {
            if (exprStatement.Expression == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(Messages.InvokeStatementNull);
                exception.Data[RuleUserDataKeys.ErrorObject] = exprStatement;
                throw exception;
            }

            RuleExpressionWalker.Decompile(decompilation, exprStatement.Expression, null);
        }
 internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
 {
     CodeFieldReferenceExpression expression2 = (CodeFieldReferenceExpression) expression;
     CodeExpression targetObject = expression2.TargetObject;
     if (targetObject == null)
     {
         RuleEvaluationException exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullFieldTarget, new object[] { expression2.FieldName }));
         exception.Data["ErrorObject"] = expression2;
         throw exception;
     }
     RuleExpressionWalker.Decompile(stringBuilder, targetObject, expression2);
     stringBuilder.Append('.');
     stringBuilder.Append(expression2.FieldName);
 }
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            CodeFieldReferenceExpression expression2 = (CodeFieldReferenceExpression)expression;
            CodeExpression targetObject = expression2.TargetObject;

            if (targetObject == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullFieldTarget, new object[] { expression2.FieldName }));
                exception.Data["ErrorObject"] = expression2;
                throw exception;
            }
            RuleExpressionWalker.Decompile(stringBuilder, targetObject, expression2);
            stringBuilder.Append('.');
            stringBuilder.Append(expression2.FieldName);
        }
Пример #15
0
 internal override void Decompile(StringBuilder decompilation)
 {
     if (this.assignStatement.Right == null)
     {
         RuleEvaluationException exception = new RuleEvaluationException(Messages.AssignRightNull);
         exception.Data["ErrorObject"] = this.assignStatement;
         throw exception;
     }
     if (this.assignStatement.Left == null)
     {
         RuleEvaluationException exception2 = new RuleEvaluationException(Messages.AssignLeftNull);
         exception2.Data["ErrorObject"] = this.assignStatement;
         throw exception2;
     }
     RuleExpressionWalker.Decompile(decompilation, this.assignStatement.Left, null);
     decompilation.Append(" = ");
     RuleExpressionWalker.Decompile(decompilation, this.assignStatement.Right, null);
 }
 internal override void Decompile(StringBuilder decompilation)
 {
     if (this.assignStatement.Right == null)
     {
         RuleEvaluationException exception = new RuleEvaluationException(Messages.AssignRightNull);
         exception.Data["ErrorObject"] = this.assignStatement;
         throw exception;
     }
     if (this.assignStatement.Left == null)
     {
         RuleEvaluationException exception2 = new RuleEvaluationException(Messages.AssignLeftNull);
         exception2.Data["ErrorObject"] = this.assignStatement;
         throw exception2;
     }
     RuleExpressionWalker.Decompile(decompilation, this.assignStatement.Left, null);
     decompilation.Append(" = ");
     RuleExpressionWalker.Decompile(decompilation, this.assignStatement.Right, null);
 }
 internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
 {
     CodeArrayIndexerExpression expression2 = (CodeArrayIndexerExpression) expression;
     object obj2 = RuleExpressionWalker.Evaluate(execution, expression2.TargetObject).Value;
     if (obj2 == null)
     {
         RuleEvaluationException exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullIndexer, new object[0]));
         exception.Data["ErrorObject"] = expression2;
         throw exception;
     }
     int count = expression2.Indices.Count;
     long[] indexerArguments = new long[count];
     for (int i = 0; i < count; i++)
     {
         Type expressionType = execution.Validation.ExpressionInfo(expression2.Indices[i]).ExpressionType;
         object operandValue = RuleExpressionWalker.Evaluate(execution, expression2.Indices[i]).Value;
         indexerArguments[i] = (long) Executor.AdjustType(expressionType, operandValue, typeof(long));
     }
     return new RuleArrayElementResult((Array) obj2, indexerArguments);
 }
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            CodeArrayIndexerExpression expression2 = (CodeArrayIndexerExpression)expression;
            object obj2 = RuleExpressionWalker.Evaluate(execution, expression2.TargetObject).Value;

            if (obj2 == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullIndexer, new object[0]));
                exception.Data["ErrorObject"] = expression2;
                throw exception;
            }
            int count = expression2.Indices.Count;

            long[] indexerArguments = new long[count];
            for (int i = 0; i < count; i++)
            {
                Type   expressionType = execution.Validation.ExpressionInfo(expression2.Indices[i]).ExpressionType;
                object operandValue   = RuleExpressionWalker.Evaluate(execution, expression2.Indices[i]).Value;
                indexerArguments[i] = (long)Executor.AdjustType(expressionType, operandValue, typeof(long));
            }
            return(new RuleArrayElementResult((Array)obj2, indexerArguments));
        }
Пример #19
0
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            CodeMethodInvokeExpression expression2 = (CodeMethodInvokeExpression)expression;

            if ((expression2.Method == null) || (expression2.Method.TargetObject == null))
            {
                RuleEvaluationException exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, new object[] { expression2.Method.MethodName }));
                exception.Data["ErrorObject"] = expression2;
                throw exception;
            }
            CodeExpression targetObject = expression2.Method.TargetObject;

            RuleExpressionWalker.Decompile(stringBuilder, targetObject, expression2);
            stringBuilder.Append('.');
            stringBuilder.Append(expression2.Method.MethodName);
            stringBuilder.Append('(');
            if (expression2.Parameters != null)
            {
                for (int i = 0; i < expression2.Parameters.Count; i++)
                {
                    CodeExpression expression4 = expression2.Parameters[i];
                    if (expression4 == null)
                    {
                        RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTypeParameter, new object[] { i.ToString(CultureInfo.CurrentCulture), expression2.Method.MethodName }));
                        exception2.Data["ErrorObject"] = expression2;
                        throw exception2;
                    }
                    if (i > 0)
                    {
                        stringBuilder.Append(", ");
                    }
                    RuleExpressionWalker.Decompile(stringBuilder, expression4, null);
                }
            }
            stringBuilder.Append(')');
        }
 internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
 {
     CodeIndexerExpression expression2 = (CodeIndexerExpression) expression;
     RulePropertyExpressionInfo info = execution.Validation.ExpressionInfo(expression2) as RulePropertyExpressionInfo;
     if (info == null)
     {
         InvalidOperationException exception = new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated, new object[0]));
         exception.Data["ErrorObject"] = expression2;
         throw exception;
     }
     PropertyInfo propertyInfo = info.PropertyInfo;
     object targetObject = RuleExpressionWalker.Evaluate(execution, expression2.TargetObject).Value;
     if (targetObject == null)
     {
         RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullIndexer, new object[0]));
         exception2.Data["ErrorObject"] = expression2;
         throw exception2;
     }
     int count = expression2.Indices.Count;
     ParameterInfo[] indexParameters = propertyInfo.GetIndexParameters();
     object[] indexerArguments = new object[indexParameters.Length];
     int length = indexParameters.Length;
     if (info.NeedsParamsExpansion)
     {
         length--;
     }
     int index = 0;
     while (index < length)
     {
         Type expressionType = execution.Validation.ExpressionInfo(expression2.Indices[index]).ExpressionType;
         RuleExpressionResult result = RuleExpressionWalker.Evaluate(execution, expression2.Indices[index]);
         indexerArguments[index] = Executor.AdjustType(expressionType, result.Value, indexParameters[index].ParameterType);
         index++;
     }
     if (length < count)
     {
         ParameterInfo info3 = indexParameters[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.Indices[index]).ExpressionType;
             RuleExpressionResult result2 = RuleExpressionWalker.Evaluate(execution, expression2.Indices[index]);
             array.SetValue(Executor.AdjustType(operandType, result2.Value, elementType), (int) (index - length));
             index++;
         }
         indexerArguments[length] = array;
     }
     return new RulePropertyResult(propertyInfo, targetObject, indexerArguments);
 }
Пример #21
0
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            CodeMethodInvokeExpression invokeExpr = (CodeMethodInvokeExpression)expression;

            if ((invokeExpr.Method == null) || (invokeExpr.Method.TargetObject == null))
            {
                string message = string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTarget, invokeExpr.Method.MethodName);
                RuleEvaluationException exception = new RuleEvaluationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = invokeExpr;
                throw exception;
            }

            // Decompile the target expression.
            CodeExpression targetObject = invokeExpr.Method.TargetObject;
            RuleExpressionWalker.Decompile(stringBuilder, targetObject, invokeExpr);

            stringBuilder.Append('.');
            stringBuilder.Append(invokeExpr.Method.MethodName);

            // Decompile the arguments
            stringBuilder.Append('(');

            if (invokeExpr.Parameters != null)
            {
                for (int i = 0; i < invokeExpr.Parameters.Count; ++i)
                {
                    CodeExpression paramExpr = invokeExpr.Parameters[i];
                    if (paramExpr == null)
                    {
                        string message = string.Format(CultureInfo.CurrentCulture, Messages.NullMethodTypeParameter, i.ToString(CultureInfo.CurrentCulture), invokeExpr.Method.MethodName);
                        RuleEvaluationException exception = new RuleEvaluationException(message);
                        exception.Data[RuleUserDataKeys.ErrorObject] = invokeExpr;
                        throw exception;
                    }

                    if (i > 0)
                        stringBuilder.Append(", ");

                    RuleExpressionWalker.Decompile(stringBuilder, paramExpr, null);
                }
            }

            stringBuilder.Append(')');
        }
Пример #22
0
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            string message;

            CodeArrayIndexerExpression arrayIndexerExpr = (CodeArrayIndexerExpression)expression;

            CodeExpression targetObject = arrayIndexerExpr.TargetObject;
            if (targetObject == null)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.NullIndexerTarget);
                RuleEvaluationException exception = new RuleEvaluationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = arrayIndexerExpr;
                throw exception;
            }

            if (arrayIndexerExpr.Indices == null || arrayIndexerExpr.Indices.Count == 0)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.MissingIndexExpressions);
                RuleEvaluationException exception = new RuleEvaluationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = arrayIndexerExpr;
                throw exception;
            }

            RuleExpressionWalker.Decompile(stringBuilder, targetObject, arrayIndexerExpr);
            stringBuilder.Append('[');
            RuleExpressionWalker.Decompile(stringBuilder, arrayIndexerExpr.Indices[0], null);
            for (int i = 1; i < arrayIndexerExpr.Indices.Count; ++i)
            {
                stringBuilder.Append(", ");
                RuleExpressionWalker.Decompile(stringBuilder, arrayIndexerExpr.Indices[i], null);
            }
            stringBuilder.Append(']');
        }
Пример #23
0
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            CodeArrayCreateExpression createExpression = (CodeArrayCreateExpression)expression;

            bool mustParenthesize = RuleDecompiler.MustParenthesize(createExpression, parentExpression);
            if (mustParenthesize)
                stringBuilder.Append("(");

            stringBuilder.Append("new ");
            RuleDecompiler.DecompileType(stringBuilder, createExpression.CreateType);
            stringBuilder.Append('[');
            if (createExpression.SizeExpression != null)
                RuleExpressionWalker.Decompile(stringBuilder, createExpression.SizeExpression, null);
            else if ((createExpression.Size != 0) || (createExpression.Initializers.Count == 0))
                stringBuilder.Append(createExpression.Size);
            stringBuilder.Append(']');
            if (createExpression.Initializers.Count > 0)
            {
                stringBuilder.Append(" { ");
                for (int i = 0; i < createExpression.Initializers.Count; ++i)
                {
                    CodeExpression paramExpr = createExpression.Initializers[i];
                    if (paramExpr == null)
                    {
                        string message = string.Format(CultureInfo.CurrentCulture, Messages.NullConstructorTypeParameter, i.ToString(CultureInfo.CurrentCulture), createExpression.CreateType);
                        RuleEvaluationException exception = new RuleEvaluationException(message);
                        exception.Data[RuleUserDataKeys.ErrorObject] = createExpression;
                        throw exception;
                    }

                    if (i > 0)
                        stringBuilder.Append(", ");

                    RuleExpressionWalker.Decompile(stringBuilder, paramExpr, null);
                }
                stringBuilder.Append('}');
            }

            if (mustParenthesize)
                stringBuilder.Append(")");
        }
 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);
 }
        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;
        }
Пример #26
0
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            string message;

            CodeMethodInvokeExpression invokeExpr = (CodeMethodInvokeExpression)expression;

            object target = RuleExpressionWalker.Evaluate(execution, invokeExpr.Method.TargetObject).Value;

            RuleMethodInvokeExpressionInfo invokeExprInfo = execution.Validation.ExpressionInfo(invokeExpr) as RuleMethodInvokeExpressionInfo;
            if (invokeExprInfo == null)  // Oops, someone forgot to validate.
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated);
                InvalidOperationException exception = new InvalidOperationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = invokeExpr;
                throw exception;
            }

            MethodInfo mi = invokeExprInfo.MethodInfo;

            if (!mi.IsStatic && target == null)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullMethod, invokeExpr.Method.MethodName);
                RuleEvaluationException exception = new RuleEvaluationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = invokeExpr;
                throw exception;
            }

            object[] arguments = null;
            RuleExpressionResult[] outArgumentResults = null;

            if (invokeExpr.Parameters != null && invokeExpr.Parameters.Count > 0)
            {
                int actualArgCount = invokeExpr.Parameters.Count;

                ParameterInfo[] parmInfos = mi.GetParameters();

                arguments = new object[parmInfos.Length];

                int numFixedParameters = parmInfos.Length;
                if (invokeExprInfo.NeedsParamsExpansion)
                    numFixedParameters -= 1;

                int i;

                // Evaluate the fixed portion of the parameter list.
                for (i = 0; i < numFixedParameters; ++i)
                {
                    Type argType = execution.Validation.ExpressionInfo(invokeExpr.Parameters[i]).ExpressionType;
                    RuleExpressionResult argResult = RuleExpressionWalker.Evaluate(execution, invokeExpr.Parameters[i]);

                    // Special procesing of direction expressions to keep track of out arguments (& ref).
                    CodeDirectionExpression direction = invokeExpr.Parameters[i] as CodeDirectionExpression;
                    if (direction != null && (direction.Direction == FieldDirection.Ref || direction.Direction == FieldDirection.Out))
                    {
                        // lazy creation of fieldsToSet
                        if (outArgumentResults == null)
                            outArgumentResults = new RuleExpressionResult[invokeExpr.Parameters.Count];
                        // keep track of this out expression so we can set it later
                        outArgumentResults[i] = argResult;

                        // don't evaluate out arguments
                        if (direction.Direction != FieldDirection.Out)
                            arguments[i] = Executor.AdjustType(argType, argResult.Value, parmInfos[i].ParameterType);
                    }
                    else
                    {
                        // treat as in
                        arguments[i] = Executor.AdjustType(argType, argResult.Value, parmInfos[i].ParameterType);
                    }
                }

                if (numFixedParameters < actualArgCount)
                {
                    // This target method had a params array, and we are calling it with an
                    // expanded parameter list.  E.g.,
                    //      void foo(int x, params string[] y)
                    // with the invocation:
                    //      foo(5, "crud", "kreeble", "glorp")
                    // We need to translate this to:
                    //      foo(5, new string[] { "crud", "kreeble", "glorp" })

                    ParameterInfo lastParamInfo = parmInfos[numFixedParameters];

                    Type arrayType = lastParamInfo.ParameterType;
                    System.Diagnostics.Debug.Assert(arrayType.IsArray);
                    Type elementType = arrayType.GetElementType();

                    Array paramsArray = (Array)arrayType.InvokeMember(arrayType.Name, BindingFlags.CreateInstance, null, null, new object[] { actualArgCount - i }, CultureInfo.CurrentCulture);
                    for (; i < actualArgCount; ++i)
                    {
                        Type argType = execution.Validation.ExpressionInfo(invokeExpr.Parameters[i]).ExpressionType;
                        RuleExpressionResult argResult = RuleExpressionWalker.Evaluate(execution, invokeExpr.Parameters[i]);
                        paramsArray.SetValue(Executor.AdjustType(argType, argResult.Value, elementType), i - numFixedParameters);
                    }

                    arguments[numFixedParameters] = paramsArray;
                }
            }

            object result;
            try
            {
                result = mi.Invoke(target, arguments);
            }
            catch (TargetInvocationException e)
            {
                // if there is no inner exception, leave it untouched
                if (e.InnerException == null)
                    throw;
                message = string.Format(CultureInfo.CurrentCulture, Messages.Error_MethodInvoke,
                    RuleDecompiler.DecompileType(mi.ReflectedType), mi.Name, e.InnerException.Message);
                throw new TargetInvocationException(message, e.InnerException);
            }

            // any out/ref parameters that need to be assigned?
            if (outArgumentResults != null)
            {
                for (int i = 0; i < invokeExpr.Parameters.Count; ++i)
                {
                    if (outArgumentResults[i] != null)
                        outArgumentResults[i].Value = arguments[i];
                }
            }

            return new RuleLiteralResult(result);
        }
Пример #27
0
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            string message;

            CodeCastExpression castExpr = (CodeCastExpression)expression;

            // Evaluate the operand.
            object operandValue = RuleExpressionWalker.Evaluate(execution, castExpr.Expression).Value;

            // Get the cast-to type.
            RuleExpressionInfo castExprInfo = execution.Validation.ExpressionInfo(castExpr);
            if (castExprInfo == null)  // Oops, someone forgot to validate.
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated);
                InvalidOperationException exception = new InvalidOperationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = castExpr;
                throw exception;
            }
            Type toType = castExprInfo.ExpressionType;

            // Handle null operand result.
            if (operandValue == null)
            {
                // Here we are casting null to something. If it is a value type we can't do it.
                if (ConditionHelper.IsNonNullableValueType(toType))
                {
                    message = string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, Messages.NullValue, RuleDecompiler.DecompileType(toType));
                    RuleEvaluationException exception = new RuleEvaluationException(message);
                    exception.Data[RuleUserDataKeys.ErrorObject] = castExpr;
                    throw exception;
                }
                // If it's not a value type, null is good.
            }
            else
            {
                Type operandType = execution.Validation.ExpressionInfo(castExpr.Expression).ExpressionType;
                operandValue = Executor.AdjustTypeWithCast(operandType, operandValue, toType);
            }

            return new RuleLiteralResult(operandValue);
        }
Пример #28
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));
        }
Пример #29
0
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            CodeCastExpression castExpr = (CodeCastExpression)expression;

            CodeExpression targetObject = castExpr.Expression;
            if (targetObject == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(Messages.NullCastExpr);
                exception.Data[RuleUserDataKeys.ErrorObject] = castExpr;
                throw exception;
            }

            if (castExpr.TargetType == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(Messages.NullCastType);
                exception.Data[RuleUserDataKeys.ErrorObject] = castExpr;
                throw exception;
            }

            bool mustParenthesize = RuleDecompiler.MustParenthesize(castExpr, parentExpression);
            if (mustParenthesize)
                stringBuilder.Append("(");

            stringBuilder.Append("(");
            RuleDecompiler.DecompileType(stringBuilder, castExpr.TargetType);
            stringBuilder.Append(")");
            RuleExpressionWalker.Decompile(stringBuilder, targetObject, castExpr);

            if (mustParenthesize)
                stringBuilder.Append(")");
        }
 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);
 }
Пример #31
0
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            bool mustParenthesize = false;
            CodeBinaryOperatorExpression binaryExpr = (CodeBinaryOperatorExpression)expression;

            if (binaryExpr.Left == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpLHS, binaryExpr.Operator.ToString());
                RuleEvaluationException exception = new RuleEvaluationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = binaryExpr;
                throw exception;
            }
            if (binaryExpr.Right == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpRHS, binaryExpr.Operator.ToString());
                RuleEvaluationException exception = new RuleEvaluationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = binaryExpr;
                throw exception;
            }

            string opString;

            switch (binaryExpr.Operator)
            {
                case CodeBinaryOperatorType.Modulus:
                    opString = " % ";
                    break;
                case CodeBinaryOperatorType.Multiply:
                    opString = " * ";
                    break;
                case CodeBinaryOperatorType.Divide:
                    opString = " / ";
                    break;

                case CodeBinaryOperatorType.Subtract:
                    opString = " - ";
                    break;
                case CodeBinaryOperatorType.Add:
                    opString = " + ";
                    break;

                case CodeBinaryOperatorType.LessThan:
                    opString = " < ";
                    break;
                case CodeBinaryOperatorType.LessThanOrEqual:
                    opString = " <= ";
                    break;
                case CodeBinaryOperatorType.GreaterThan:
                    opString = " > ";
                    break;
                case CodeBinaryOperatorType.GreaterThanOrEqual:
                    opString = " >= ";
                    break;

                case CodeBinaryOperatorType.IdentityEquality:
                case CodeBinaryOperatorType.ValueEquality:
                    opString = " == ";
                    break;
                case CodeBinaryOperatorType.IdentityInequality:
                    opString = " != ";
                    break;

                case CodeBinaryOperatorType.BitwiseAnd:
                    opString = " & ";
                    break;

                case CodeBinaryOperatorType.BitwiseOr:
                    opString = " | ";
                    break;

                case CodeBinaryOperatorType.BooleanAnd:
                    opString = " && ";
                    break;

                case CodeBinaryOperatorType.BooleanOr:
                    opString = " || ";
                    break;

                default:
                    string message = string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, binaryExpr.Operator.ToString());
                    NotSupportedException exception = new NotSupportedException(message);
                    exception.Data[RuleUserDataKeys.ErrorObject] = binaryExpr;
                    throw exception;
            }

            CodeExpression leftExpr = binaryExpr.Left;
            CodeExpression rightExpr = binaryExpr.Right;


            if (binaryExpr.Operator == CodeBinaryOperatorType.ValueEquality)
            {
                // Look for special cases:
                //    LHS == false              --> ! LHS
                // or
                //    (LHS == expr) == false    --> LHS != expr

                CodePrimitiveExpression rhsPrimitive = rightExpr as CodePrimitiveExpression;
                if (rhsPrimitive != null)
                {
                    object rhsValue = rhsPrimitive.Value;
                    if (rhsValue != null)
                    {
                        // we don't have the comparison "==null"
                        if (rhsValue.GetType() == typeof(bool) && (bool)rhsValue == false)
                        {
                            // We have comparison "== false".

                            CodeBinaryOperatorExpression lhsBinary = leftExpr as CodeBinaryOperatorExpression;
                            if (lhsBinary != null && lhsBinary.Operator == CodeBinaryOperatorType.ValueEquality)
                            {
                                // We have the pattern
                                //      (expr1 == expr2) == false
                                // Treat this as:
                                //      expr1 != expr2

                                opString = " != ";

                                leftExpr = lhsBinary.Left;
                                rightExpr = lhsBinary.Right;
                            }
                            else
                            {
                                // We have the pattern
                                //      LHS == false
                                // Treat this as:
                                //      ! LHS

                                mustParenthesize = RuleDecompiler.MustParenthesize(leftExpr, parentExpression);
                                if (mustParenthesize)
                                    stringBuilder.Append("(");

                                // Note the "parentExpression" passed to the child decompile... cast is the only
                                // built-in operation that has "unary" precedence, so pass that as the parent
                                // to get the parenthesization right. .
                                stringBuilder.Append("!");
                                RuleExpressionWalker.Decompile(stringBuilder, leftExpr, new CodeCastExpression());

                                if (mustParenthesize)
                                    stringBuilder.Append(")");

                                return;
                            }
                        }
                    }
                }
            }
            else if (binaryExpr.Operator == CodeBinaryOperatorType.Subtract)
            {
                // Look for the special case:
                //    0 - RHS       --> - RHS

                CodePrimitiveExpression lhsPrimitive = leftExpr as CodePrimitiveExpression;
                if (lhsPrimitive != null && lhsPrimitive.Value != null)
                {
                    object lhsValue = lhsPrimitive.Value;

                    // Check if the LHS is zero.  We'll only check a few types (decimal,
                    // double, float, int, long), since these occur most often (and the 
                    // unsigned types are all illegal).
                    TypeCode tc = Type.GetTypeCode(lhsValue.GetType());
                    bool isZero = false;
                    switch (tc)
                    {
                        case TypeCode.Decimal:
                            isZero = ((decimal)lhsValue) == 0;
                            break;

                        case TypeCode.Double:
                            isZero = ((double)lhsValue) == 0;
                            break;

                        case TypeCode.Single:
                            isZero = ((float)lhsValue) == 0;
                            break;

                        case TypeCode.Int32:
                            isZero = ((int)lhsValue) == 0;
                            break;

                        case TypeCode.Int64:
                            isZero = ((long)lhsValue) == 0;
                            break;
                    }

                    if (isZero)
                    {
                        mustParenthesize = RuleDecompiler.MustParenthesize(rightExpr, parentExpression);
                        if (mustParenthesize)
                            stringBuilder.Append("(");

                        // Note the "parentExpression" passed to the child decompile... cast is the only
                        // built-in operation that has "unary" precedence, so pass that as the parent
                        // to get the parenthesization right.  
                        stringBuilder.Append("-");
                        RuleExpressionWalker.Decompile(stringBuilder, rightExpr, new CodeCastExpression());

                        if (mustParenthesize)
                            stringBuilder.Append(")");

                        return;
                    }
                }
            }

            mustParenthesize = RuleDecompiler.MustParenthesize(binaryExpr, parentExpression);
            if (mustParenthesize)
                stringBuilder.Append("(");

            RuleExpressionWalker.Decompile(stringBuilder, leftExpr, binaryExpr);
            stringBuilder.Append(opString);
            RuleExpressionWalker.Decompile(stringBuilder, rightExpr, binaryExpr);

            if (mustParenthesize)
                stringBuilder.Append(")");
        }
Пример #32
0
        private static object EvaluateBinaryOperation(CodeBinaryOperatorExpression binaryExpr, Type lhsType, object lhsValue, CodeBinaryOperatorType operation, Type rhsType, object rhsValue)
        {
            Literal leftLiteral;
            Literal rightLiteral;
            ArithmeticLiteral leftArithmetic;
            ArithmeticLiteral rightArithmetic;
            string message;
            RuleEvaluationException exception;

            switch (operation)
            {
                case CodeBinaryOperatorType.Add:
                    leftArithmetic = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (leftArithmetic == null)
                        break;
                    rightArithmetic = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (rightArithmetic == null)
                        break;
                    return leftArithmetic.Add(rightArithmetic);
                case CodeBinaryOperatorType.Subtract:
                    leftArithmetic = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (leftArithmetic == null)
                        break;
                    rightArithmetic = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (rightArithmetic == null)
                        break;
                    return leftArithmetic.Subtract(rightArithmetic);
                case CodeBinaryOperatorType.Multiply:
                    leftArithmetic = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (leftArithmetic == null)
                        break;
                    rightArithmetic = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (rightArithmetic == null)
                        break;
                    return leftArithmetic.Multiply(rightArithmetic);
                case CodeBinaryOperatorType.Divide:
                    leftArithmetic = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (leftArithmetic == null)
                        break;
                    rightArithmetic = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (rightArithmetic == null)
                        break;
                    return leftArithmetic.Divide(rightArithmetic);
                case CodeBinaryOperatorType.Modulus:
                    leftArithmetic = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (leftArithmetic == null)
                        break;
                    rightArithmetic = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (rightArithmetic == null)
                        break;
                    return leftArithmetic.Modulus(rightArithmetic);
                case CodeBinaryOperatorType.BitwiseAnd:
                    leftArithmetic = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (leftArithmetic == null)
                        break;
                    rightArithmetic = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (rightArithmetic == null)
                        break;
                    return leftArithmetic.BitAnd(rightArithmetic);
                case CodeBinaryOperatorType.BitwiseOr:
                    leftArithmetic = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue);
                    if (leftArithmetic == null)
                        break;
                    rightArithmetic = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue);
                    if (rightArithmetic == null)
                        break;
                    return leftArithmetic.BitOr(rightArithmetic);

                case CodeBinaryOperatorType.ValueEquality:
                    leftLiteral = Literal.MakeLiteral(lhsType, lhsValue);
                    if (leftLiteral == null)
                        break;
                    rightLiteral = Literal.MakeLiteral(rhsType, rhsValue);
                    if (rightLiteral == null)
                        break;
                    return leftLiteral.Equal(rightLiteral);
                case CodeBinaryOperatorType.IdentityEquality:
                    return lhsValue == rhsValue;
                case CodeBinaryOperatorType.IdentityInequality:
                    return lhsValue != rhsValue;

                case CodeBinaryOperatorType.LessThan:
                    leftLiteral = Literal.MakeLiteral(lhsType, lhsValue);
                    if (leftLiteral == null)
                        break;
                    rightLiteral = Literal.MakeLiteral(rhsType, rhsValue);
                    if (rightLiteral == null)
                        break;
                    return leftLiteral.LessThan(rightLiteral);
                case CodeBinaryOperatorType.LessThanOrEqual:
                    leftLiteral = Literal.MakeLiteral(lhsType, lhsValue);
                    if (leftLiteral == null)
                        break;
                    rightLiteral = Literal.MakeLiteral(rhsType, rhsValue);
                    if (rightLiteral == null)
                        break;
                    return leftLiteral.LessThanOrEqual(rightLiteral);
                case CodeBinaryOperatorType.GreaterThan:
                    leftLiteral = Literal.MakeLiteral(lhsType, lhsValue);
                    if (leftLiteral == null)
                        break;
                    rightLiteral = Literal.MakeLiteral(rhsType, rhsValue);
                    if (rightLiteral == null)
                        break;
                    return leftLiteral.GreaterThan(rightLiteral);
                case CodeBinaryOperatorType.GreaterThanOrEqual:
                    leftLiteral = Literal.MakeLiteral(lhsType, lhsValue);
                    if (leftLiteral == null)
                        break;
                    rightLiteral = Literal.MakeLiteral(rhsType, rhsValue);
                    if (rightLiteral == null)
                        break;
                    return leftLiteral.GreaterThanOrEqual(rightLiteral);

                default:
                    // should never happen
                    // BooleanAnd & BooleanOr short-circuited before call
                    // Assign disallowed at validation time
                    message = string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, operation.ToString());
                    exception = new RuleEvaluationException(message);
                    exception.Data[RuleUserDataKeys.ErrorObject] = binaryExpr;
                    throw exception;
            }

            message = string.Format(CultureInfo.CurrentCulture,
                Messages.BinaryOpFails,
                operation.ToString(),
                RuleDecompiler.DecompileType(lhsType),
                RuleDecompiler.DecompileType(rhsType));
            exception = new RuleEvaluationException(message);
            exception.Data[RuleUserDataKeys.ErrorObject] = binaryExpr;
            throw exception;
        }
        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 void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            string str3;
            bool flag = false;
            CodeBinaryOperatorExpression childExpr = (CodeBinaryOperatorExpression) expression;
            if (childExpr.Left == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpLHS, new object[] { childExpr.Operator.ToString() }));
                exception.Data["ErrorObject"] = childExpr;
                throw exception;
            }
            if (childExpr.Right == null)
            {
                RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpRHS, new object[] { childExpr.Operator.ToString() }));
                exception2.Data["ErrorObject"] = childExpr;
                throw exception2;
            }
            switch (childExpr.Operator)
            {
                case CodeBinaryOperatorType.Add:
                    str3 = " + ";
                    break;

                case CodeBinaryOperatorType.Subtract:
                    str3 = " - ";
                    break;

                case CodeBinaryOperatorType.Multiply:
                    str3 = " * ";
                    break;

                case CodeBinaryOperatorType.Divide:
                    str3 = " / ";
                    break;

                case CodeBinaryOperatorType.Modulus:
                    str3 = " % ";
                    break;

                case CodeBinaryOperatorType.IdentityInequality:
                    str3 = " != ";
                    break;

                case CodeBinaryOperatorType.IdentityEquality:
                case CodeBinaryOperatorType.ValueEquality:
                    str3 = " == ";
                    break;

                case CodeBinaryOperatorType.BitwiseOr:
                    str3 = " | ";
                    break;

                case CodeBinaryOperatorType.BitwiseAnd:
                    str3 = " & ";
                    break;

                case CodeBinaryOperatorType.BooleanOr:
                    str3 = " || ";
                    break;

                case CodeBinaryOperatorType.BooleanAnd:
                    str3 = " && ";
                    break;

                case CodeBinaryOperatorType.LessThan:
                    str3 = " < ";
                    break;

                case CodeBinaryOperatorType.LessThanOrEqual:
                    str3 = " <= ";
                    break;

                case CodeBinaryOperatorType.GreaterThan:
                    str3 = " > ";
                    break;

                case CodeBinaryOperatorType.GreaterThanOrEqual:
                    str3 = " >= ";
                    break;

                default:
                {
                    NotSupportedException exception3 = new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { childExpr.Operator.ToString() }));
                    exception3.Data["ErrorObject"] = childExpr;
                    throw exception3;
                }
            }
            CodeExpression left = childExpr.Left;
            CodeExpression right = childExpr.Right;
            if (childExpr.Operator == CodeBinaryOperatorType.ValueEquality)
            {
                CodePrimitiveExpression expression5 = right as CodePrimitiveExpression;
                if (expression5 != null)
                {
                    object obj2 = expression5.Value;
                    if (((obj2 != null) && (obj2.GetType() == typeof(bool))) && !((bool) obj2))
                    {
                        CodeBinaryOperatorExpression expression6 = left as CodeBinaryOperatorExpression;
                        if ((expression6 == null) || (expression6.Operator != CodeBinaryOperatorType.ValueEquality))
                        {
                            flag = RuleDecompiler.MustParenthesize(left, parentExpression);
                            if (flag)
                            {
                                stringBuilder.Append("(");
                            }
                            stringBuilder.Append("!");
                            RuleExpressionWalker.Decompile(stringBuilder, left, new CodeCastExpression());
                            if (flag)
                            {
                                stringBuilder.Append(")");
                            }
                            return;
                        }
                        str3 = " != ";
                        left = expression6.Left;
                        right = expression6.Right;
                    }
                }
            }
            else if (childExpr.Operator == CodeBinaryOperatorType.Subtract)
            {
                CodePrimitiveExpression expression7 = left as CodePrimitiveExpression;
                if ((expression7 != null) && (expression7.Value != null))
                {
                    object obj3 = expression7.Value;
                    TypeCode typeCode = Type.GetTypeCode(obj3.GetType());
                    bool flag2 = false;
                    switch (typeCode)
                    {
                        case TypeCode.Int32:
                            flag2 = ((int) obj3) == 0;
                            break;

                        case TypeCode.Int64:
                            flag2 = ((long) obj3) == 0L;
                            break;

                        case TypeCode.Single:
                            flag2 = ((float) obj3) == 0f;
                            break;

                        case TypeCode.Double:
                            flag2 = ((double) obj3) == 0.0;
                            break;

                        case TypeCode.Decimal:
                            flag2 = ((decimal) obj3) == 0M;
                            break;
                    }
                    if (flag2)
                    {
                        flag = RuleDecompiler.MustParenthesize(right, parentExpression);
                        if (flag)
                        {
                            stringBuilder.Append("(");
                        }
                        stringBuilder.Append("-");
                        RuleExpressionWalker.Decompile(stringBuilder, right, new CodeCastExpression());
                        if (flag)
                        {
                            stringBuilder.Append(")");
                        }
                        return;
                    }
                }
            }
            flag = RuleDecompiler.MustParenthesize(childExpr, parentExpression);
            if (flag)
            {
                stringBuilder.Append("(");
            }
            RuleExpressionWalker.Decompile(stringBuilder, left, childExpr);
            stringBuilder.Append(str3);
            RuleExpressionWalker.Decompile(stringBuilder, right, childExpr);
            if (flag)
            {
                stringBuilder.Append(")");
            }
        }
        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));
        }
Пример #36
0
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            CodePropertyReferenceExpression propGetExpr = (CodePropertyReferenceExpression)expression;

            CodeExpression targetObject = propGetExpr.TargetObject;
            if (targetObject == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Messages.NullPropertyTarget, propGetExpr.PropertyName);
                RuleEvaluationException exception = new RuleEvaluationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = propGetExpr;
                throw exception;
            }

            RuleExpressionWalker.Decompile(stringBuilder, targetObject, propGetExpr);
            stringBuilder.Append('.');
            stringBuilder.Append(propGetExpr.PropertyName);
        }
Пример #37
0
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            CodeArrayIndexerExpression arrayIndexerExpr = (CodeArrayIndexerExpression)expression;

            // Evaluate the target...
            object target = RuleExpressionWalker.Evaluate(execution, arrayIndexerExpr.TargetObject).Value;

            if (target == null)
            {
                string message = string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullIndexer);
                RuleEvaluationException exception = new RuleEvaluationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = arrayIndexerExpr;
                throw exception;
            }

            // Evaluate the index arguments (converting them to "longs")
            int actualArgCount = arrayIndexerExpr.Indices.Count;
            long[] indexArgs = new long[actualArgCount];

            for (int i = 0; i < actualArgCount; ++i)
            {
                Type argType = execution.Validation.ExpressionInfo(arrayIndexerExpr.Indices[i]).ExpressionType;
                object argValue = RuleExpressionWalker.Evaluate(execution, arrayIndexerExpr.Indices[i]).Value;
                indexArgs[i] = (long)Executor.AdjustType(argType, argValue, typeof(long));
            }

            RuleArrayElementResult result = new RuleArrayElementResult((Array)target, indexArgs);
            return result;
        }
        internal override void Decompile(CodeExpression expression, StringBuilder stringBuilder, CodeExpression parentExpression)
        {
            string str3;
            bool   flag = false;
            CodeBinaryOperatorExpression childExpr = (CodeBinaryOperatorExpression)expression;

            if (childExpr.Left == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpLHS, new object[] { childExpr.Operator.ToString() }));
                exception.Data["ErrorObject"] = childExpr;
                throw exception;
            }
            if (childExpr.Right == null)
            {
                RuleEvaluationException exception2 = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpRHS, new object[] { childExpr.Operator.ToString() }));
                exception2.Data["ErrorObject"] = childExpr;
                throw exception2;
            }
            switch (childExpr.Operator)
            {
            case CodeBinaryOperatorType.Add:
                str3 = " + ";
                break;

            case CodeBinaryOperatorType.Subtract:
                str3 = " - ";
                break;

            case CodeBinaryOperatorType.Multiply:
                str3 = " * ";
                break;

            case CodeBinaryOperatorType.Divide:
                str3 = " / ";
                break;

            case CodeBinaryOperatorType.Modulus:
                str3 = " % ";
                break;

            case CodeBinaryOperatorType.IdentityInequality:
                str3 = " != ";
                break;

            case CodeBinaryOperatorType.IdentityEquality:
            case CodeBinaryOperatorType.ValueEquality:
                str3 = " == ";
                break;

            case CodeBinaryOperatorType.BitwiseOr:
                str3 = " | ";
                break;

            case CodeBinaryOperatorType.BitwiseAnd:
                str3 = " & ";
                break;

            case CodeBinaryOperatorType.BooleanOr:
                str3 = " || ";
                break;

            case CodeBinaryOperatorType.BooleanAnd:
                str3 = " && ";
                break;

            case CodeBinaryOperatorType.LessThan:
                str3 = " < ";
                break;

            case CodeBinaryOperatorType.LessThanOrEqual:
                str3 = " <= ";
                break;

            case CodeBinaryOperatorType.GreaterThan:
                str3 = " > ";
                break;

            case CodeBinaryOperatorType.GreaterThanOrEqual:
                str3 = " >= ";
                break;

            default:
            {
                NotSupportedException exception3 = new NotSupportedException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { childExpr.Operator.ToString() }));
                exception3.Data["ErrorObject"] = childExpr;
                throw exception3;
            }
            }
            CodeExpression left  = childExpr.Left;
            CodeExpression right = childExpr.Right;

            if (childExpr.Operator == CodeBinaryOperatorType.ValueEquality)
            {
                CodePrimitiveExpression expression5 = right as CodePrimitiveExpression;
                if (expression5 != null)
                {
                    object obj2 = expression5.Value;
                    if (((obj2 != null) && (obj2.GetType() == typeof(bool))) && !((bool)obj2))
                    {
                        CodeBinaryOperatorExpression expression6 = left as CodeBinaryOperatorExpression;
                        if ((expression6 == null) || (expression6.Operator != CodeBinaryOperatorType.ValueEquality))
                        {
                            flag = RuleDecompiler.MustParenthesize(left, parentExpression);
                            if (flag)
                            {
                                stringBuilder.Append("(");
                            }
                            stringBuilder.Append("!");
                            RuleExpressionWalker.Decompile(stringBuilder, left, new CodeCastExpression());
                            if (flag)
                            {
                                stringBuilder.Append(")");
                            }
                            return;
                        }
                        str3  = " != ";
                        left  = expression6.Left;
                        right = expression6.Right;
                    }
                }
            }
            else if (childExpr.Operator == CodeBinaryOperatorType.Subtract)
            {
                CodePrimitiveExpression expression7 = left as CodePrimitiveExpression;
                if ((expression7 != null) && (expression7.Value != null))
                {
                    object   obj3     = expression7.Value;
                    TypeCode typeCode = Type.GetTypeCode(obj3.GetType());
                    bool     flag2    = false;
                    switch (typeCode)
                    {
                    case TypeCode.Int32:
                        flag2 = ((int)obj3) == 0;
                        break;

                    case TypeCode.Int64:
                        flag2 = ((long)obj3) == 0L;
                        break;

                    case TypeCode.Single:
                        flag2 = ((float)obj3) == 0f;
                        break;

                    case TypeCode.Double:
                        flag2 = ((double)obj3) == 0.0;
                        break;

                    case TypeCode.Decimal:
                        flag2 = ((decimal)obj3) == 0M;
                        break;
                    }
                    if (flag2)
                    {
                        flag = RuleDecompiler.MustParenthesize(right, parentExpression);
                        if (flag)
                        {
                            stringBuilder.Append("(");
                        }
                        stringBuilder.Append("-");
                        RuleExpressionWalker.Decompile(stringBuilder, right, new CodeCastExpression());
                        if (flag)
                        {
                            stringBuilder.Append(")");
                        }
                        return;
                    }
                }
            }
            flag = RuleDecompiler.MustParenthesize(childExpr, parentExpression);
            if (flag)
            {
                stringBuilder.Append("(");
            }
            RuleExpressionWalker.Decompile(stringBuilder, left, childExpr);
            stringBuilder.Append(str3);
            RuleExpressionWalker.Decompile(stringBuilder, right, childExpr);
            if (flag)
            {
                stringBuilder.Append(")");
            }
        }
Пример #39
0
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            CodeObjectCreateExpression createExpression = (CodeObjectCreateExpression)expression;

            if (createExpression.CreateType == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(Messages.NullTypeType);
                exception.Data[RuleUserDataKeys.ErrorObject] = createExpression;
                throw exception;
            }

            RuleExpressionInfo expressionInfo = execution.Validation.ExpressionInfo(createExpression);
            if (expressionInfo == null)  // Oops, someone forgot to validate.
            {
                InvalidOperationException exception = new InvalidOperationException(Messages.ExpressionNotValidated);
                exception.Data[RuleUserDataKeys.ErrorObject] = createExpression;
                throw exception;
            }

            RuleConstructorExpressionInfo createExpressionInfo = expressionInfo as RuleConstructorExpressionInfo;
            if (createExpressionInfo == null)
            {
                // it's just a regular RuleExpressionInfo, which means this is a value-type with no parameters
                return new RuleLiteralResult(Activator.CreateInstance(expressionInfo.ExpressionType));
            }

            ConstructorInfo constructor = createExpressionInfo.ConstructorInfo;
            object[] arguments = null;
            RuleExpressionResult[] outArgumentResults = null;

            if (createExpression.Parameters != null && createExpression.Parameters.Count > 0)
            {
                int actualArgCount = createExpression.Parameters.Count;
                ParameterInfo[] parmInfos = constructor.GetParameters();

                arguments = new object[parmInfos.Length];

                int numFixedParameters = parmInfos.Length;
                if (createExpressionInfo.NeedsParamsExpansion)
                    numFixedParameters -= 1;

                int i;

                // Evaluate the fixed portion of the parameter list.
                for (i = 0; i < numFixedParameters; ++i)
                {
                    Type argType = execution.Validation.ExpressionInfo(createExpression.Parameters[i]).ExpressionType;
                    RuleExpressionResult argResult = RuleExpressionWalker.Evaluate(execution, createExpression.Parameters[i]);

                    // Special procesing of direction expressions to keep track of out arguments (& ref).
                    CodeDirectionExpression direction = createExpression.Parameters[i] as CodeDirectionExpression;
                    if (direction != null && (direction.Direction == FieldDirection.Ref || direction.Direction == FieldDirection.Out))
                    {
                        // lazy creation of fieldsToSet
                        if (outArgumentResults == null)
                            outArgumentResults = new RuleExpressionResult[actualArgCount];
                        // keep track of this out expression so we can set it later
                        outArgumentResults[i] = argResult;
                    }

                    arguments[i] = Executor.AdjustType(argType, argResult.Value, parmInfos[i].ParameterType);
                }

                if (numFixedParameters < actualArgCount)
                {
                    // This target method had a params array, and we are calling it with an
                    // expanded parameter list.  E.g.,
                    //      void foo(int x, params string[] y)
                    // with the invocation:
                    //      foo(5, "crud", "kreeble", "glorp")
                    // We need to translate this to:
                    //      foo(5, new string[] { "crud", "kreeble", "glorp" })

                    ParameterInfo lastParamInfo = parmInfos[numFixedParameters];

                    Type arrayType = lastParamInfo.ParameterType;
                    System.Diagnostics.Debug.Assert(arrayType.IsArray);
                    Type elementType = arrayType.GetElementType();

                    Array paramsArray = Array.CreateInstance(elementType, actualArgCount - i);
                    for (; i < actualArgCount; ++i)
                    {
                        Type argType = execution.Validation.ExpressionInfo(createExpression.Parameters[i]).ExpressionType;
                        RuleExpressionResult argResult = RuleExpressionWalker.Evaluate(execution, createExpression.Parameters[i]);
                        paramsArray.SetValue(Executor.AdjustType(argType, argResult.Value, elementType), i - numFixedParameters);
                    }

                    arguments[numFixedParameters] = paramsArray;
                }
            }

            object result;
            try
            {
                result = constructor.Invoke(arguments);
            }
            catch (TargetInvocationException e)
            {
                // if there is no inner exception, leave it untouched
                if (e.InnerException == null)
                    throw;
                string message = string.Format(CultureInfo.CurrentCulture,
                    Messages.Error_ConstructorInvoke,
                    RuleDecompiler.DecompileType(createExpressionInfo.ExpressionType),
                    e.InnerException.Message);
                throw new TargetInvocationException(message, e.InnerException);
            }

            // any out/ref parameters that need to be assigned?
            if (outArgumentResults != null)
            {
                for (int i = 0; i < createExpression.Parameters.Count; ++i)
                {
                    if (outArgumentResults[i] != null)
                        outArgumentResults[i].Value = arguments[i];
                }
            }
            return new RuleLiteralResult(result);
        }
 internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
 {
     CodeArrayCreateExpression expression2 = (CodeArrayCreateExpression) expression;
     if (expression2.CreateType == null)
     {
         RuleEvaluationException exception = new RuleEvaluationException(Messages.NullTypeType);
         exception.Data["ErrorObject"] = expression2;
         throw exception;
     }
     RuleExpressionInfo info = execution.Validation.ExpressionInfo(expression2);
     if (expression2 == null)
     {
         InvalidOperationException exception2 = new InvalidOperationException(Messages.ExpressionNotValidated);
         exception2.Data["ErrorObject"] = expression2;
         throw exception2;
     }
     Type elementType = info.ExpressionType.GetElementType();
     int length = 0;
     if (expression2.SizeExpression != null)
     {
         Type expressionType = execution.Validation.ExpressionInfo(expression2.SizeExpression).ExpressionType;
         RuleExpressionResult result = RuleExpressionWalker.Evaluate(execution, expression2.SizeExpression);
         if (expressionType == typeof(int))
         {
             length = (int) result.Value;
         }
         else if (expressionType == typeof(long))
         {
             length = (int) ((long) result.Value);
         }
         else if (expressionType == typeof(uint))
         {
             length = (int) ((uint) result.Value);
         }
         else if (expressionType == typeof(ulong))
         {
             length = (int) ((ulong) result.Value);
         }
     }
     else if (expression2.Size != 0)
     {
         length = expression2.Size;
     }
     else
     {
         length = expression2.Initializers.Count;
     }
     Array literal = Array.CreateInstance(elementType, length);
     if (expression2.Initializers != null)
     {
         for (int i = 0; i < expression2.Initializers.Count; i++)
         {
             CodeExpression expression3 = expression2.Initializers[i];
             Type operandType = execution.Validation.ExpressionInfo(expression3).ExpressionType;
             RuleExpressionResult result2 = RuleExpressionWalker.Evaluate(execution, expression3);
             literal.SetValue(Executor.AdjustType(operandType, result2.Value, elementType), i);
         }
     }
     return new RuleLiteralResult(literal);
 }
Пример #41
0
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            CodeArrayCreateExpression createExpression = (CodeArrayCreateExpression)expression;

            if (createExpression.CreateType == null)
            {
                RuleEvaluationException exception = new RuleEvaluationException(Messages.NullTypeType);
                exception.Data[RuleUserDataKeys.ErrorObject] = createExpression;
                throw exception;
            }

            RuleExpressionInfo createExpressionInfo = execution.Validation.ExpressionInfo(createExpression);
            if (createExpression == null)  // Oops, someone forgot to validate.
            {
                InvalidOperationException exception = new InvalidOperationException(Messages.ExpressionNotValidated);
                exception.Data[RuleUserDataKeys.ErrorObject] = createExpression;
                throw exception;
            }

            // type should be an array type already
            Type type = createExpressionInfo.ExpressionType;
            Type elementType = type.GetElementType();

            // assume this has been validated, so only 1 size specified
            int size = 0;
            if (createExpression.SizeExpression != null)
            {
                Type sizeType = execution.Validation.ExpressionInfo(createExpression.SizeExpression).ExpressionType;
                RuleExpressionResult sizeResult = RuleExpressionWalker.Evaluate(execution, createExpression.SizeExpression);
                if (sizeType == typeof(int))
                    size = (int)sizeResult.Value;
                else if (sizeType == typeof(long))
                    size = (int)((long)sizeResult.Value);
                else if (sizeType == typeof(uint))
                    size = (int)((uint)sizeResult.Value);
                else if (sizeType == typeof(ulong))
                    size = (int)((ulong)sizeResult.Value);
            }
            else if (createExpression.Size != 0)
                size = createExpression.Size;
            else
                size = createExpression.Initializers.Count;

            Array result = Array.CreateInstance(elementType, size);
            if (createExpression.Initializers != null)
                for (int i = 0; i < createExpression.Initializers.Count; ++i)
                {
                    CodeExpression initializer = createExpression.Initializers[i];
                    Type initializerType = execution.Validation.ExpressionInfo(initializer).ExpressionType;
                    RuleExpressionResult initializerResult = RuleExpressionWalker.Evaluate(execution, initializer);
                    result.SetValue(Executor.AdjustType(initializerType, initializerResult.Value, elementType), i);
                }
            return new RuleLiteralResult(result);
        }
Пример #42
0
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            CodeArrayCreateExpression expression2 = (CodeArrayCreateExpression)expression;

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

            if (expression2 == null)
            {
                InvalidOperationException exception2 = new InvalidOperationException(Messages.ExpressionNotValidated);
                exception2.Data["ErrorObject"] = expression2;
                throw exception2;
            }
            Type elementType = info.ExpressionType.GetElementType();
            int  length      = 0;

            if (expression2.SizeExpression != null)
            {
                Type expressionType         = execution.Validation.ExpressionInfo(expression2.SizeExpression).ExpressionType;
                RuleExpressionResult result = RuleExpressionWalker.Evaluate(execution, expression2.SizeExpression);
                if (expressionType == typeof(int))
                {
                    length = (int)result.Value;
                }
                else if (expressionType == typeof(long))
                {
                    length = (int)((long)result.Value);
                }
                else if (expressionType == typeof(uint))
                {
                    length = (int)((uint)result.Value);
                }
                else if (expressionType == typeof(ulong))
                {
                    length = (int)((ulong)result.Value);
                }
            }
            else if (expression2.Size != 0)
            {
                length = expression2.Size;
            }
            else
            {
                length = expression2.Initializers.Count;
            }
            Array literal = Array.CreateInstance(elementType, length);

            if (expression2.Initializers != null)
            {
                for (int i = 0; i < expression2.Initializers.Count; i++)
                {
                    CodeExpression       expression3 = expression2.Initializers[i];
                    Type                 operandType = execution.Validation.ExpressionInfo(expression3).ExpressionType;
                    RuleExpressionResult result2     = RuleExpressionWalker.Evaluate(execution, expression3);
                    literal.SetValue(Executor.AdjustType(operandType, result2.Value, elementType), i);
                }
            }
            return(new RuleLiteralResult(literal));
        }
Пример #43
0
        internal override RuleExpressionResult Evaluate(CodeExpression expression, RuleExecution execution)
        {
            string message;

            CodeIndexerExpression indexerExpr = (CodeIndexerExpression)expression;

            RulePropertyExpressionInfo propExprInfo = execution.Validation.ExpressionInfo(indexerExpr) as RulePropertyExpressionInfo;
            if (propExprInfo == null)  // Oops, someone forgot to validate.
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.ExpressionNotValidated);
                InvalidOperationException exception = new InvalidOperationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = indexerExpr;
                throw exception;
            }

            PropertyInfo pi = propExprInfo.PropertyInfo;

            // Evaluate the target...
            object target = RuleExpressionWalker.Evaluate(execution, indexerExpr.TargetObject).Value;

            if (target == null)
            {
                message = string.Format(CultureInfo.CurrentCulture, Messages.TargetEvaluatedNullIndexer);
                RuleEvaluationException exception = new RuleEvaluationException(message);
                exception.Data[RuleUserDataKeys.ErrorObject] = indexerExpr;
                throw exception;
            }

            // Evaluate the index arguments.
            int actualArgCount = indexerExpr.Indices.Count;
            ParameterInfo[] parmInfos = pi.GetIndexParameters();
            object[] indexArgs = new object[parmInfos.Length];

            int numFixedParameters = parmInfos.Length;
            if (propExprInfo.NeedsParamsExpansion)
                numFixedParameters -= 1;

            int i;
            for (i = 0; i < numFixedParameters; ++i)
            {
                Type argType = execution.Validation.ExpressionInfo(indexerExpr.Indices[i]).ExpressionType;
                RuleExpressionResult argResult = RuleExpressionWalker.Evaluate(execution, indexerExpr.Indices[i]);
                indexArgs[i] = Executor.AdjustType(argType, argResult.Value, parmInfos[i].ParameterType);
            }

            if (numFixedParameters < actualArgCount)
            {
                // This target indexer had a params array, and we are calling it with an
                // expanded parameter list.  E.g.,
                //      int this[int x, params string[] y]
                // with the invocation:
                //      x.y[5, "crud", "kreeble", "glorp"]
                // We need to translate this to:
                //      x.y[5, new string[] { "crud", "kreeble", "glorp" }]

                ParameterInfo lastParamInfo = parmInfos[numFixedParameters];

                Type arrayType = lastParamInfo.ParameterType;
                System.Diagnostics.Debug.Assert(arrayType.IsArray);
                Type elementType = arrayType.GetElementType();

                Array paramsArray = (Array)arrayType.InvokeMember(arrayType.Name, BindingFlags.CreateInstance, null, null, new object[] { actualArgCount - i }, CultureInfo.CurrentCulture);
                for (; i < actualArgCount; ++i)
                {
                    Type argType = execution.Validation.ExpressionInfo(indexerExpr.Indices[i]).ExpressionType;
                    RuleExpressionResult argResult = RuleExpressionWalker.Evaluate(execution, indexerExpr.Indices[i]);
                    paramsArray.SetValue(Executor.AdjustType(argType, argResult.Value, elementType), i - numFixedParameters);
                }

                indexArgs[numFixedParameters] = paramsArray;
            }

            RulePropertyResult result = new RulePropertyResult(pi, target, indexArgs);
            return result;
        }