Example #1
0
        public void FuncOneArgCached()
        {
            var reflectionCache = new ReflectionCache();

            var info = typeof(Foo).GetMethod(nameof(Foo.Method1Arg));
            var func = reflectionCache.GetMethodInvoker(info);

            Assert.IsNotNull(func);
            var func2 = reflectionCache.GetMethodInvoker(info);

            Assert.IsNotNull(func2);

            Assert.AreEqual(func, func2);
        }
Example #2
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);
        }
Example #3
0
        public void SetterFuncCached()
        {
            var reflectionCache = new ReflectionCache();

            var propertyInfo = typeof(Foo).GetProperty(nameof(Foo.Property1));
            var func         = reflectionCache.GetPropertySetter(propertyInfo);

            Assert.IsNotNull(func);
            var func2 = reflectionCache.GetPropertySetter(propertyInfo);

            Assert.IsNotNull(func2);

            Assert.AreEqual(func, func2);
        }
Example #4
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);
        }
Example #5
0
        public void GetPropertyFuncGenericAndSetValue()
        {
            var reflectionCache = new ReflectionCache();

            var propertyInfo = typeof(Foo).GetProperty(nameof(Foo.Property1));
            var func         = reflectionCache.GetPropertySetter <string>(propertyInfo);

            Assert.IsNotNull(func);

            var          foo            = new Foo();
            const string expectedValue1 = "Foo";

            func(foo, expectedValue1);

            Assert.AreEqual(expectedValue1, foo.Property1);
        }
Example #6
0
        public void GetMethodFuncGenericWithNoArgsAndCallIt()
        {
            var reflectionCache = new ReflectionCache();

            var info = typeof(Foo).GetMethod(nameof(Foo.Method0Args));
            var func = reflectionCache.GetMethodInvoker <string>(info);

            Assert.IsNotNull(func);

            var foo = new Foo();

            var funcResult = func(foo, new object[] { });

            Assert.IsTrue(foo.Method0ArgsCalled);
            Assert.AreEqual(Foo.Method0ArgsResult, funcResult,
                            "Func didn't return the expected value.");
        }
Example #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);
        }
Example #8
0
        public void GetPropertyFuncAndRetrieveValue()
        {
            var reflectionCache = new ReflectionCache();

            var propertyInfo = typeof(Foo).GetProperty(nameof(Foo.Property1));
            var func         = reflectionCache.GetPropertyGetter(propertyInfo);

            Assert.IsNotNull(func);

            var          foo            = new Foo();
            const string expectedValue1 = "Foo";

            foo.Property1 = expectedValue1;

            var property1 = func(foo);

            Assert.AreEqual(expectedValue1, property1);
        }
Example #9
0
        public void GetMethodFuncWithOneArgAndCallIt()
        {
            var reflectionCache = new ReflectionCache();

            var info = typeof(Foo).GetMethod(nameof(Foo.Method1Arg));
            var func = reflectionCache.GetMethodInvoker(info);

            Assert.IsNotNull(func);

            var foo = new Foo();

            const string expectedValue1 = "Foo";
            var          funcResult     = func(foo, new object[] { expectedValue1 });

            Assert.IsTrue(foo.Method1ArgCalled);
            Assert.AreEqual(expectedValue1, foo.Arg1);

            Assert.AreEqual(expectedValue1, funcResult,
                            "Func didn't return the expected value.");
        }
Example #10
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 });
        }
        public void FuncOneArgCached()
        {
            var reflectionCache = new ReflectionCache();
            var info            = typeof(Foo).GetMethod(nameof(Foo.Method1Arg));

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

            void InvokeUsingMode(DelegateCreationMode mode)
            {
                var func = reflectionCache.GetMethodInvoker(info, mode);

                Assert.IsNotNull(func, mode.ToString());
                var func2 = reflectionCache.GetMethodInvoker(info, mode);

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

                Assert.AreEqual(func, func2, mode.ToString());
            }
        }
Example #12
0
        public void ActionZeroArgsCached()
        {
            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 func2 = reflectionCache.GetVoidMethodInvoker(info, mode);

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

                Assert.AreEqual(action, func2, mode.ToString());
            }
        }
Example #13
0
        public void GetterFuncCached()
        {
            var reflectionCache = new ReflectionCache();

            var propertyInfo = typeof(Foo).GetProperty(nameof(Foo.Property1));

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

            void InvokeUsingMode(DelegateCreationMode mode)
            {
                var func = reflectionCache.GetPropertyGetter(propertyInfo, mode);

                Assert.IsNotNull(func, mode.ToString());
                var func2 = reflectionCache.GetPropertyGetter(propertyInfo, mode);

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

                Assert.AreEqual(func, func2, mode.ToString());
            }
        }
        public void GetMethodFuncWithThreeArgsAndCallIt()
        {
            var reflectionCache = new ReflectionCache();

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

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

            void InvokeUsingMode(DelegateCreationMode mode)
            {
                var func = reflectionCache.GetMethodInvoker(info, mode);

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

                var foo = new Foo();

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

                var funcResult = func(foo, new object[]
                {
                    expectedValue1, expectedValue2, expectedValue3
                });

                Assert.IsTrue(foo.Method3ArgsCalled, mode.ToString());
                Assert.AreEqual(expectedValue1, foo.Arg1, mode.ToString());
                Assert.AreEqual(expectedValue2, foo.Arg2, mode.ToString());
                Assert.AreEqual(expectedValue3, foo.Arg3, mode.ToString());

                Assert.AreEqual(expectedValue1, funcResult,
                                "Func didn't return the expected value." + mode);
            }
        }
Example #15
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());
            }
        }