Esempio n. 1
0
        public void TestInvokeSubtractDynamic()
        {
            var tType = ExpressionType.Subtract;
            var tMock = CreateMock(tType);

            Impromptu.InvokeBinaryOperator(tMock, tType, 4);
        }
Esempio n. 2
0
 /// <summary>
 /// Forwards the Binary operation
 /// </summary>
 /// <param name="binder"></param>
 /// <param name="arg"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result)
 {
     result = null;
     try
     {
         result = Impromptu.InvokeBinaryOperator(CallTarget, binder.Operation, arg);
         return(true);
     }
     catch (RuntimeBinderException)
     {
         return(false);
     }
 }
Esempio n. 3
0
            public object GetResult(Invocation invocation)
            {
                (new PermissionSet(PermissionState.Unrestricted)).Assert();
                object result = null;
                string name   = invocation.Name.Name;

                object[] args       = invocation.Args;
                Type[]   paramTypes = args.Select(o => o.GetType()).ToArray();
                try
                {
                    if (invocation.Kind == InvocationKind.NotSet && invocation.Name.Name == "_BinaryOperator")
                    { // We use that for operators
                        var    exp   = (ExpressionType)args[0];
                        object other = args[1];
                        result = Impromptu.InvokeBinaryOperator(_component, exp, other);
                    }
                    else if (invocation.Kind == InvocationKind.NotSet && invocation.Name.Name == "_UnaryOperator")
                    { // We use that for operators
                        var exp = (ExpressionType)args[0];
                        result = Impromptu.InvokeUnaryOperator(exp, _component);
                    }
                    else
                    {
                        // First we try to resolve via DLR
                        dynamic target = _component;
                        result = invocation.InvokeWithStoredArgs(_component);
                    }
                }
                catch (RuntimeBinderException)
                {
                    // DLR doesn't like some kind of functions,
                    // expecially because we have casted the component to object...
                    bool found = false;
                    switch (invocation.Kind)
                    {
                    case InvocationKind.Convert:
                        var  targetType = (Type)args[0];
                        bool tExplict   = false;
                        if (args.Length == 2)
                        {
                            tExplict = (bool)args[1];
                        }
                        // try to find explicit or implicit operator.
                        try
                        {
                            result = DynamicCast(_component, targetType);
                            found  = true;
                        }
                        catch (Exception)
                        {
                            found = false;
                        }
                        break;

                    //case InvocationKind.IsEvent:
                    //case InvocationKind.AddAssign:
                    //case InvocationKind.SubtractAssign:
                    //case InvocationKind.Invoke:
                    //case InvocationKind.InvokeAction:
                    //case InvocationKind.InvokeUnknown:
                    //case InvocationKind.InvokeMember: // TODO: add testcase
                    //case InvocationKind.InvokeMemberAction: // TODO: add testcase
                    //case InvocationKind.GetIndex: // TODO: add testcase
                    //case InvocationKind.SetIndex: // TODO: add testcase
                    case InvocationKind.Get:
                    case InvocationKind.Set:
                    case InvocationKind.InvokeMemberUnknown:
                    {
                        if (!found)
                        {
                            if (!TryFindInvokeMember(_runtimeType, name, args, paramTypes, out result))
                            {
                                // search all interfaces as well
                                foreach (var @interface in _runtimeType.GetInterfaces().Where(i => i.IsPublic))
                                {
                                    if (TryFindInvokeMember(@interface, name, args, paramTypes, out result))
                                    {
                                        found = true;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                found = true;
                            }
                        }
                    }
                    break;

                    default:
                        break;
                    }

                    if (!found)
                    {
                        if (_allowMissing)
                        {
                            return(RazorDynamicObject.Create("", _allowMissing));
                        }
                        throw;
                    }
                }
                if (RazorDynamicObject.IsPrimitive(result))
                {
                    return(result);
                }
                else
                {
                    return(RazorDynamicObject.Create(result, _allowMissing));
                }
            }
Esempio n. 4
0
 public void TestInvokeSubtract()
 {
     Assert.AreEqual(Impromptu.InvokeBinaryOperator(1, ExpressionType.Subtract, 2), -1);
 }
Esempio n. 5
0
        public void TestInvokeAddDynamic()
        {
            var tMock = CreateMock(ExpressionType.Add);

            Impromptu.InvokeBinaryOperator(tMock, ExpressionType.Add, 4);
        }
Esempio n. 6
0
 public void TestInvokeAdd()
 {
     Assert.AreEqual(Impromptu.InvokeBinaryOperator(1, ExpressionType.Add, 2), 3);
 }