public void GetMethodProxy_NullInput_ThrowsArgumentNullException()
        {
            ArgumentNullException ex = Assert.Throws <ArgumentNullException>(
                delegate()
            {
                ProxyDelegate setter = DynamicMethodGenerator.GetMethodProxy(null);
            });

            Assert.Equal("methodInfo", ex.ParamName);
        }
        public void GetMethodProxy_MethodNoArgsOneReturn_BuildsProxyAndInvokes()
        {
            var input = new Example();

            ProxyDelegate proxy = DynamicMethodGenerator.GetMethodProxy(typeof(Example).GetMethod("GetMi"));

            Assert.NotNull(proxy);
            var actual = (string)proxy(input);

            Assert.Equal("me", actual);
        }
        public void GetMethodProxy_ArgsTypeMismatchWhenCalling_ThrowsArgumentNullException()
        {
            ProxyDelegate proxy = DynamicMethodGenerator.GetMethodProxy(typeof(Example).GetMethod("Reset"));

            Assert.NotNull(proxy);

            InvalidCastException ex = Assert.Throws <InvalidCastException>(
                delegate()
            {
                proxy(new Example(), 1, 2, 3, 4, 5, 6, 7, 8, 9);
            });
        }
        public void GetMethodProxy_ArgsMissingWhenCalling_ThrowsArgumentNullException()
        {
            ProxyDelegate proxy = DynamicMethodGenerator.GetMethodProxy(typeof(Example).GetMethod("Reset"));

            Assert.NotNull(proxy);

            ArgumentException ex = Assert.Throws <ArgumentException>(
                delegate()
            {
                var actual = proxy(new Example(), "alpha", "bravo", "charlie", -1, -2, -3);
            });
        }
        public void GetMethodProxy_MethodOneArgOneReturn_BuildsProxyAndInvokes()
        {
            var input = new Example();

            ProxyDelegate proxy = DynamicMethodGenerator.GetMethodProxy(typeof(Example).GetMethod("Swap"));

            Assert.NotNull(proxy);
            var actual = (string)proxy(input, "foo");

            Assert.Equal("aye", actual);
            Assert.Equal("foo", input.A);
        }
        public void GetMethodProxy_1MillionMethodCalls_PerformsInAround50ms()
        {
            Example    instance   = new Example();
            MethodInfo methodInfo = typeof(Example).GetMethod("GetMi", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            ProxyDelegate proxy = DynamicMethodGenerator.GetMethodProxy(methodInfo);

            string value = null;

            for (long i = 0; i < MaxCount; i++)
            {
                value = (string)proxy(instance);
            }
        }
        public void GetMethodProxy_MethodExtraArgs_IgnoresExtraBuildsProxyAndInvokes()
        {
            var input    = new Example();
            var expected = new Example("alpha", "bravo", "charlie", -1, -2, -3, "deer", "sun", "myself");

            ProxyDelegate proxy = DynamicMethodGenerator.GetMethodProxy(typeof(Example).GetMethod("Reset"));

            Assert.NotNull(proxy);
            proxy(input, "alpha", "bravo", "charlie", -1, -2, -3, "deer", "sun", "myself", 4, 5, 6, "extra", false);

            var getters =
                from m in typeof(Example).GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy)
                let g = DynamicMethodGenerator.GetGetter(m)
                        where (g != null)
                        select g;

            foreach (GetterDelegate getter in getters)
            {
                // assert all of the fields and properties are equal
                Assert.Equal(getter(expected), getter(input));
            }
        }
Пример #8
0
        public static ProxyDelegate GetMethodProxy(Type declaringType, string methodName, params Type[] argTypes)
        {
            if (declaringType == null)
            {
                throw new ArgumentNullException("declaringType");
            }
            if (String.IsNullOrEmpty(methodName))
            {
                throw new ArgumentNullException("methodName");
            }

            MethodInfo methodInfo;

            if (argTypes.Length > 0)
            {
                methodInfo = declaringType.GetMethod(
                    methodName,
                    BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy,
                    null,
                    argTypes,
                    null);
            }
            else
            {
                methodInfo = declaringType.GetMethod(
                    methodName,
                    BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
            }

            if (methodInfo == null)
            {
                return(null);
            }

            return(DynamicMethodGenerator.GetMethodProxy(methodInfo));
        }