Exemplo n.º 1
0
        /// <summary>
        /// Forwards the invoke operation.
        /// </summary>
        /// <param name="binder">the binder</param>
        /// <param name="value">the value</param>
        /// <returns>true when successfull</returns>
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (CallTarget == null)
            {
                return(false);
            }

            if (Impromptu.InvokeIsEvent(CallTarget, binder.Name) && value is ImpromptuForwarderAddRemove)
            {
                var tValue = value as ImpromptuForwarderAddRemove;

                if (tValue.IsAdding)
                {
                    Impromptu.InvokeAddAssignMember(CallTarget, binder.Name, tValue.Delegate);
                }
                else
                {
                    Impromptu.InvokeSubtractAssignMember(CallTarget, binder.Name, tValue.Delegate);
                }

                return(true);
            }

            try
            {
                Impromptu.InvokeSet(CallTarget, binder.Name, value);

                return(true);
            }
            catch (RuntimeBinderException)
            {
                return(false);
            }
        }
Exemplo n.º 2
0
        public void TestDynamicAddAssign()
        {
            var  tDynamic = Build.NewObject(Prop2: 3, Event: null, OnEvent: new ThisAction <object, EventArgs>((@this, obj, args) => @this.Event(obj, args)));
            bool tTest    = false;

            Impromptu.InvokeAddAssignMember(tDynamic, "Event", new EventHandler <EventArgs>((@object, args) => { tTest = true; }));

            tDynamic.OnEvent(null, null);

            Assert.AreEqual(true, tTest);

            Impromptu.InvokeAddAssignMember(tDynamic, "Prop2", 4);

            Assert.AreEqual(7L, tDynamic.Prop2);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Registers or unregister.
        /// </summary>
        /// <param name="un">if set to <c>true</c> [un].</param>
        /// <param name="source">The source.</param>
        /// <param name="eventName">Name of the event.</param>
        /// <param name="targetName">Name of the target.</param>
        private void RegisterUnRegister(bool un, object source, string eventName, string targetName)
        {
            if (Impromptu.InvokeIsEvent(source, eventName))
            {
                var tEvent = source.GetType().GetEvent(eventName);


                var tEventHandler = Event.GenerateEventHandler(tEvent.EventHandlerType, targetName);

                if (un)
                {
                    Impromptu.InvokeSubtractAssignMember(source, eventName, tEventHandler);
                }
                else
                {
                    Impromptu.InvokeAddAssignMember(source, eventName, tEventHandler);
                }
            }
        }
Exemplo n.º 4
0
        public void TestPocoAddAssign()
        {
            var  tPoco = new PocoEvent();
            bool tTest = false;

            Impromptu.InvokeAddAssignMember(tPoco, "Event", new EventHandler <EventArgs>((@object, args) => { tTest = true; }));

            tPoco.OnEvent(null, null);

            Assert.AreEqual(true, tTest);

            var tPoco2 = new PropPoco()
            {
                Prop2 = 3
            };

            Impromptu.InvokeAddAssignMember(tPoco2, "Prop2", 4);

            Assert.AreEqual(7L, tPoco2.Prop2);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Invokes the invocation on specified target with specific args.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <param name="args">The args.</param>
        /// <returns></returns>
        public virtual object Invoke(object target, params object[] args)
        {
            switch (Kind)
            {
            case InvocationKind.Constructor:
                return(Impromptu.InvokeConstructor((Type)target, args));

            case InvocationKind.Convert:
                bool tExplict = false;
                if (Args.Length == 2)
                {
                    tExplict = (bool)args[1];
                }
                return(Impromptu.InvokeConvert(target, (Type)args[0], tExplict));

            case InvocationKind.Get:
                return(Impromptu.InvokeGet(target, Name.Name));

            case InvocationKind.Set:
                Impromptu.InvokeSet(target, Name.Name, args.FirstOrDefault());
                return(null);

            case InvocationKind.GetIndex:
                return(Impromptu.InvokeGetIndex(target, args));

            case InvocationKind.SetIndex:
                Impromptu.InvokeSetIndex(target, args);
                return(null);

            case InvocationKind.InvokeMember:
                return(Impromptu.InvokeMember(target, Name, args));

            case InvocationKind.InvokeMemberAction:
                Impromptu.InvokeMemberAction(target, Name, args);
                return(null);

            case InvocationKind.InvokeMemberUnknown:
            {
                try
                {
                    return(Impromptu.InvokeMember(target, Name, args));
                }
                catch (RuntimeBinderException)
                {
                    Impromptu.InvokeMemberAction(target, Name, args);
                    return(null);
                }
            }

            case InvocationKind.Invoke:
                return(Impromptu.Invoke(target, args));

            case InvocationKind.InvokeAction:
                Impromptu.InvokeAction(target, args);
                return(null);

            case InvocationKind.InvokeUnknown:
            {
                try
                {
                    return(Impromptu.Invoke(target, args));
                }
                catch (RuntimeBinderException)
                {
                    Impromptu.InvokeAction(target, args);
                    return(null);
                }
            }

            case InvocationKind.AddAssign:
                Impromptu.InvokeAddAssignMember(target, Name.Name, args.FirstOrDefault());
                return(null);

            case InvocationKind.SubtractAssign:
                Impromptu.InvokeSubtractAssignMember(target, Name.Name, args.FirstOrDefault());
                return(null);

            case InvocationKind.IsEvent:
                return(Impromptu.InvokeIsEvent(target, Name.Name));

            default:
                throw new InvalidOperationException("Unknown Invocation Kind: " + Kind);
            }
        }