コード例 #1
0
        private object Invoke(BinaryExpression node, string opName)
        {
            if (opName is null)
            {
                ExecutionException.ThrowInvalidOp(node);
            }
            var left  = node.Left.Accept(this);
            var right = node.Right.Accept(this);

            return(Invoke(node, opName, left, right));
        }
コード例 #2
0
        static object Invoke(BinaryExpression node, string opName, object left, object right)
        {
            if (left is null)
            {
                ExecutionException.ThrowNullError(node.Left, node);
            }
            if (right is null)
            {
                ExecutionException.ThrowNullError(node.Right, node);
            }

            if (node.Method is null)
            {
                var leftType  = left.GetType();
                var rightType = right.GetType();
                var types     = new Type[2] {
                    leftType, rightType
                };
                ArgumentConversions          conversions = new ArgumentConversions(2);
                System.Reflection.MethodInfo method;
                if (leftType.IsPrimitive || rightType.IsPrimitive)
                {
                    var initial = (ReflectionUtils.FromSystemType(ref types));
                    method = ReflectionUtils.
                             GetOperatorOverload(opName, conversions, types);
                    conversions.SetInitial(initial);
                }
                else
                {
                    method = ReflectionUtils.
                             GetOperatorOverload(opName, conversions, types);
                }
                if (method is null)
                {
                    ExecutionException.ThrowInvalidOp(node);
                }
                node.Method      = method;
                node.Type        = method.ReturnType;
                node.Conversions = conversions;
            }
            object[] args = new object[2] {
                left, right
            };
            node.Conversions.Invoke(ref args);
            // operator overload invoke
            return(node.Method.Invoke(null, System.Reflection.BindingFlags.Default, null, args, null));
        }
コード例 #3
0
 /// <summary>
 /// Safe compiler invoke for exceptions
 /// </summary>
 /// <param name="exp">Invoking expression</param>
 /// <param name="method">method to Invoke</param>
 /// <param name="target">method target</param>
 /// <param name="parmeters">parameters for invoke</param>
 /// <returns>result of invoke</returns>
 public static object SafeInvoke(Expression exp, System.Reflection.MethodBase method, object target, object[] parmeters)
 {
     try
     {
         return(method.Invoke(target, System.Reflection.BindingFlags.Default, null, parmeters, null));
     }
     catch (System.Reflection.TargetInvocationException)
     {
         throw ExecutionException.ThrowInvalidOp(exp);
     }
     catch (ArgumentException ex)
     {
         throw ExecutionException.ThrowInvalidOp(exp, new NameExpression(ex.ParamName, ExpressionType.Identifier));
     }
     catch (System.Reflection.TargetParameterCountException)
     {
         throw ExecutionException.ThrowArgumentMisMatch(exp);
     }
 }
コード例 #4
0
        public object VisitUnary(UnaryExpression node)
        {
            var value = node.Operand.Accept(this);

            if (node.NodeType == ExpressionType.Parenthesized)
            {
                node.Type = node.Operand.Type;
                return(value);
            }
            //modified a++; updated new value
            bool modified = false, updated = true;

            switch (node.NodeType)
            {
            case ExpressionType.PostfixPlusPlus:
                modified = true;
                updated  = false;
                break;

            case ExpressionType.PrefixPlusPlus:
                modified = true;
                break;

            case ExpressionType.PostfixMinusMinus:
                modified = true;
                updated  = false;
                break;

            case ExpressionType.PrefixMinusMinus:
                modified = true;
                break;

            case ExpressionType.Bang:
                // here value is null it is as not defined
                if (value is null)
                {
                    return(Boolean.True);
                }
                break;
            }
            if (value is null)
            {
                ExecutionException.ThrowNullError(node.Operand, node);
            }
            // no primitive supported it should be wrapped

            //resolve call
            if (node.Method is null)
            {
                Type type = node.Operand.Type;
                ArgumentConversions          conversions = new ArgumentConversions(1);
                System.Reflection.MethodInfo method;
                if (type.IsPrimitive)
                {
                    type   = TypeProvider.Find(Type.GetTypeCode(type));
                    method = ReflectionUtils.GetOperatorOverload(node.MethodName, conversions, type);
                    conversions.AddFirst(new ParamConversion(0, ReflectionHelpers.ToAny));
                }
                else
                {
                    method = ReflectionUtils.GetOperatorOverload(node.MethodName, conversions, type);
                }
                if (method is null)
                {
                    ExecutionException.ThrowInvalidOp(node);
                }
                node.Conversions = conversions;
                node.Method      = method;
                node.Type        = method.ReturnType;
            }
            object[] args = new object[1] {
                value
            };
            node.Conversions.Invoke(ref args);
            object obj = node.Method.Invoke(null, args);

            if (modified)
            {
                if (node.Operand.NodeType == ExpressionType.Literal)
                {
                    ExecutionException.ThrowNotSupported(node);
                }
                var exp = new AssignmentExpression(node.Operand, new LiteralExpression(obj));
                exp.Accept(this);
            }
            return(updated ? obj : value);
        }