Beispiel #1
0
        public void ActionOneArgCached()
        {
            var reflectionCache = new ReflectionCache();

            var info   = typeof(Foo).GetMethod(nameof(Foo.MethodVoid1Arg));
            var action = reflectionCache.GetVoidMethodInvoker(info);

            Assert.IsNotNull(action);
            var func2 = reflectionCache.GetVoidMethodInvoker(info);

            Assert.IsNotNull(func2);

            Assert.AreEqual(action, func2);
        }
Beispiel #2
0
        public void GetMethodActionWithTwoArgsAndCallIt()
        {
            var reflectionCache = new ReflectionCache();

            var info = typeof(Foo).GetMethod(nameof(Foo.MethodVoid2Args));

            foreach (DelegateCreationMode mode in delegateCreationModes)
            {
                InvokeUsingMode(mode);
            }

            void InvokeUsingMode(DelegateCreationMode mode)
            {
                var action = reflectionCache.GetVoidMethodInvoker(info, mode);

                Assert.IsNotNull(action, mode.ToString());

                var foo = new Foo();

                const string expectedValue1 = "One";
                const string expectedValue2 = "Two";

                action(foo, new object[] { expectedValue1, expectedValue2 });

                Assert.IsTrue(foo.MethodVoid2ArgsCalled, mode.ToString());
                Assert.AreEqual(expectedValue1, foo.Arg1, mode.ToString());
                Assert.AreEqual(expectedValue2, foo.Arg2, mode.ToString());
            }
        }
Beispiel #3
0
        internal static Action AddHandler(object target, string eventName, Action <object, EventArgs> methodToExecute)
        {
#if NETSTANDARD
            var eventInfo = target.GetType().GetRuntimeEvent(eventName);
#else
            var eventInfo = target.GetType().GetEvent(eventName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
#endif
            if (eventInfo == null)
            {
                throw new ArgumentException(
                          nameof(eventName) + " " + eventName
                          + " does not exist on target of type:"
                          + target.GetType());
            }

            var delegateType   = eventInfo.EventHandlerType;
            var dynamicHandler = BuildDynamicHandler(delegateType, methodToExecute);

#if NETSTANDARD
            var methodInfo = eventInfo.AddMethod;
#else
            var methodInfo = eventInfo.GetAddMethod();
#endif
            var invoker = ReflectionCache.GetVoidMethodInvoker(
                methodInfo,
                DelegateCreationMode.FastCreationSlowPerformance);

            invoker(target, new object[] { dynamicHandler });

            void Result() => RemoveHandler(target, eventName, dynamicHandler);

            return(Result);
        }
Beispiel #4
0
        public void GetMethodActionWithThreeArgsAndCallIt()
        {
            var reflectionCache = new ReflectionCache();

            var info   = typeof(Foo).GetMethod(nameof(Foo.MethodVoid3Args));
            var action = reflectionCache.GetVoidMethodInvoker(info);

            Assert.IsNotNull(action);

            var foo = new Foo();

            const string expectedValue1 = "One";
            const string expectedValue2 = "Two";
            const string expectedValue3 = "Two";

            action(foo, new object[]
            {
                expectedValue1, expectedValue2, expectedValue3
            });

            Assert.IsTrue(foo.MethodVoid3ArgsCalled);
            Assert.AreEqual(expectedValue1, foo.Arg1);
            Assert.AreEqual(expectedValue2, foo.Arg2);
            Assert.AreEqual(expectedValue3, foo.Arg3);
        }
Beispiel #5
0
        public void ActionOneArgCached()
        {
            var reflectionCache = new ReflectionCache();

            var info = typeof(Foo).GetMethod(nameof(Foo.MethodVoid1Arg));

            foreach (DelegateCreationMode mode in delegateCreationModes)
            {
                InvokeUsingMode(mode);
            }

            void InvokeUsingMode(DelegateCreationMode mode)
            {
                var action = reflectionCache.GetVoidMethodInvoker(info, mode);

                Assert.IsNotNull(action, mode.ToString());
                var func2 = reflectionCache.GetVoidMethodInvoker(info, mode);

                Assert.IsNotNull(func2, mode.ToString());

                Assert.AreEqual(action, func2, mode.ToString());
            }
        }
Beispiel #6
0
        public void GetMethodActionWithNoArgsAndCallIt()
        {
            var reflectionCache = new ReflectionCache();

            var info   = typeof(Foo).GetMethod(nameof(Foo.MethodVoid0Args));
            var action = reflectionCache.GetVoidMethodInvoker(info);

            Assert.IsNotNull(action);

            var foo = new Foo();

            action(foo, new object[] { });

            Assert.IsTrue(foo.MethodVoid0ArgsCalled);
        }
Beispiel #7
0
        public void GetMethodActionWithOneArgAndCallIt()
        {
            var reflectionCache = new ReflectionCache();

            var info   = typeof(Foo).GetMethod(nameof(Foo.MethodVoid1Arg));
            var action = reflectionCache.GetVoidMethodInvoker(info);

            Assert.IsNotNull(action);

            var foo = new Foo();

            const string expectedValue1 = "Foo";

            action(foo, new object[] { expectedValue1 });

            Assert.IsTrue(foo.MethodVoid1ArgCalled);
            Assert.AreEqual(expectedValue1, foo.Arg1);
        }
Beispiel #8
0
        internal static void RemoveHandler(object target, string eventName, Delegate dynamicHandler)
        {
#if NETSTANDARD
            var eventInfo = target.GetType().GetRuntimeEvent(eventName);
#else
            var eventInfo = target.GetType().GetEvent(eventName);
#endif
            if (eventInfo == null)
            {
                throw new ArgumentException(nameof(eventName) + "does not exist on target of type:" + target.GetType());
            }

#if NETSTANDARD
            MethodInfo methodInfo = eventInfo.RemoveMethod;
#else
            MethodInfo methodInfo = eventInfo.GetRemoveMethod();
#endif
            var invoker = ReflectionCache.GetVoidMethodInvoker(
                methodInfo,
                DelegateCreationMode.FastCreationSlowPerformance);
            invoker(target, new object[] { dynamicHandler });
        }
Beispiel #9
0
        public void GetMethodActionWithNoArgsAndCallIt()
        {
            var reflectionCache = new ReflectionCache();

            var info = typeof(Foo).GetMethod(nameof(Foo.MethodVoid0Args));

            foreach (DelegateCreationMode mode in delegateCreationModes)
            {
                InvokeUsingMode(mode);
            }

            void InvokeUsingMode(DelegateCreationMode mode)
            {
                var action = reflectionCache.GetVoidMethodInvoker(info, mode);

                Assert.IsNotNull(action, mode.ToString());

                var foo = new Foo();

                action(foo, new object[] { });

                Assert.IsTrue(foo.MethodVoid0ArgsCalled, mode.ToString());
            }
        }