Example #1
0
        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.InvokeAddAssign(CallTarget, binder.Name, tValue.Delegate);
                }
                else
                {
                    Impromptu.InvokeSubtractAssign(CallTarget, binder.Name, tValue.Delegate);
                }

                return(true);
            }

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

            return(true);
        }
        /// <summary>
        /// Provides the implementation for operations that get member values. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations such as getting a value for a property.
        /// </summary>
        /// <param name="binder">Provides information about the object that called the dynamic operation. The binder.Name property provides the name of the member on which the dynamic operation is performed. For example, for the Console.WriteLine(sampleObject.SampleProperty) statement, where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Name returns "SampleProperty". The binder.IgnoreCase property specifies whether the member name is case-sensitive.</param>
        /// <param name="result">The result of the get operation. For example, if the method is called for a property, you can assign the property value to <paramref name="result"/>.</param>
        /// <returns>
        /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a run-time exception is thrown.)
        /// </returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (CallTarget == null)
            {
                result = null;
                return(false);
            }

            if (Impromptu.InvokeIsEvent(CallTarget, binder.Name))
            {
                result = new ImpromptuForwarderAddRemove();
                return(true);
            }

            try
            {
                result = Impromptu.InvokeGet(CallTarget, binder.Name);
            }
            catch (RuntimeBinderException)
            {
                result = null;
                return(false);
            }

            return(true);
        }
Example #3
0
        public void TestIsNotEvent()
        {
            dynamic tDynamic = new ImpromptuDictionary();

            tDynamic.Event = null;

            var tResult = Impromptu.InvokeIsEvent(tDynamic, "Event");

            Assert.AreEqual(false, tResult);

            bool tTest  = false;
            bool tTest2 = false;


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

            tDynamic.Event += new EventHandler <EventArgs>((@object, args) => { tTest2 = true; });

            Assert.AreEqual(false, tTest);

            Assert.AreEqual(false, tTest2);

            tDynamic.Event(null, null);

            Assert.AreEqual(true, tTest);

            Assert.AreEqual(true, tTest2);
        }
Example #4
0
        public void TestIsEvent()
        {
            dynamic tPoco = new PocoEvent();

            var tResult = Impromptu.InvokeIsEvent(tPoco, "Event");

            Assert.AreEqual(true, tResult);
        }
Example #5
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);
                }
            }
        }
Example #6
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);
            }
        }