예제 #1
0
 /// <summary>
 /// Forwards the unary operation.
 /// </summary>
 /// <param name="binder"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 public override bool TryUnaryOperation(UnaryOperationBinder binder, out object result)
 {
     result = null;
     try
     {
         result = Impromptu.InvokeUnaryOperator(binder.Operation, CallTarget);
         return(true);
     }
     catch (RuntimeBinderException)
     {
         return(false);
     }
 }
예제 #2
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));
                }
            }