Exemplo n.º 1
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));
        }
Exemplo n.º 2
0
        private object VisitCompare(BinaryExpression node, string opName)
        {
            var left  = node.Left.Accept(this);
            var right = node.Right.Accept(this);

            if (left is null || right is null)
            {
                return(NullCompare(node, left, right));
            }
            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)
                {
                    return(NullCompare(node, left, right));
                }
                node.Method      = method;
                node.Conversions = conversions;
                node.Type        = method.ReturnType;
            }
            // null method handled
            object[] args = new object[2] {
                left, right
            };
            node.Conversions.Invoke(ref args);
            return(node.Method.Invoke(null, args));
        }
Exemplo n.º 3
0
        static System.Reflection.MethodInfo VisitBinary(Expression left, Expression right, string opName, ArgumentConversions conversions)
        {
            var types = new Type[2] {
                left.Type, right.Type
            };

            System.Reflection.MethodInfo methodInfo;
            if (left.Type.IsPrimitive || right.Type.IsPrimitive)
            {
                Conversion[] initial = ReflectionUtils.FromSystemType(ref types);
                methodInfo = ReflectionUtils.
                             GetOperatorOverload(opName, conversions, types);
                // add initial conversion
                conversions.SetInitial(initial);
            }
            else
            {
                methodInfo = ReflectionUtils.
                             GetOperatorOverload(opName, conversions, types);
            }
            return(methodInfo);
        }
Exemplo n.º 4
0
        static System.Reflection.MethodInfo VisitPow(BinaryExpression node, Expression left, Expression right, ArgumentConversions conversions)
        {
            var types = new Type[2] {
                left.Type, right.Type
            };

            System.Reflection.MethodInfo method = ReflectionHelpers.MathPow;
            if (left.Type.IsPrimitive || right.Type.IsPrimitive)
            {
                var initial = ReflectionUtils.FromSystemType(ref types);
                if (!method.MatchesArgumentTypes(types, conversions))
                {
                    ExecutionException.ThrowArgumentMisMatch(node);
                }
                conversions.SetInitial(initial);
                return(method);
            }
            if (!method.MatchesArgumentTypes(types, conversions))
            {
                ExecutionException.ThrowArgumentMisMatch(node);
            }
            return(method);
        }
Exemplo n.º 5
0
        private object VisitExponentiation(BinaryExpression node)
        {
            var left  = node.Left.Accept(this);
            var right = node.Right.Accept(this);

            if (left is null)
            {
                ExecutionException.ThrowNullError(node, node.Left);
            }
            if (right is null)
            {
                ExecutionException.ThrowNullError(node, node.Right);
            }
            object[] args = new object[2] {
                left, right
            };
            if (node.Method is null)
            {
                Type leftType    = left.GetType();
                Type rightType   = right.GetType();
                var  types       = new Type[] { leftType, rightType };
                var  conversions = new ArgumentConversions(2);
                if (leftType.IsPrimitive || rightType.IsPrimitive)
                {
                    conversions.SetInitial(ReflectionUtils.FromSystemType(ref types));
                }
                if (!ReflectionHelpers.MathPow.MatchesArgumentTypes(types, conversions))
                {
                    ExecutionException.ThrowArgumentMisMatch(node);
                }
                node.Conversions = conversions;
                node.Method      = ReflectionHelpers.MathPow;
                node.Type        = TypeProvider.DoubleType;
            }
            node.Conversions.Invoke(ref args);
            return(node.Method.Invoke(null, args));
        }