Ejemplo n.º 1
0
        private static void RunCastleDynamicProxy()
        {
            var proxy = new Castle.DynamicProxy.ProxyGenerator().CreateInterfaceProxyWithTarget <IProxyImplementInterface>(new TestProxyImplementInterface(), new ValidateInterceptor(), new LogInterceptor());

            CodeTimerAdvance.TimeByConsole("Castle.DynamicProxy.TestMethodWithRefAndOutParameter", COUNT, times =>
            {
                var a      = 1;
                var b      = new string[] { "2" };
                var c      = 0L;
                var result = proxy.TestMethodWithRefAndOutParameter(ref a, ref b, out c);
                if (result != c)
                {
                    throw new ApplicationException("Castle.DynamicProxy.TestMethodWithRefAndOutParameter fail");
                }
            });
            CodeTimerAdvance.TimeByConsole("Castle.DynamicProxy.TestNormalMethod", COUNT, times =>
            {
                var a      = 1;
                var b      = "2";
                var result = proxy.TestNormalMethod(a, b);
                if (result != (a + 1) + Convert.ToInt32(b))
                {
                    throw new ApplicationException($"Castle.DynamicProxy.TestNormalMethod fail, actual:{result}, expected:{(a + 1) + Convert.ToInt32(b)}");
                }
            });
            CodeTimerAdvance.TimeByConsole("Castle.DynamicProxy.TestMethodWithGenericParameter", COUNT, times =>
            {
                var a = 1;
                var b = "2";
                var c = new ValueA {
                    Value = 3
                };
                var result = proxy.TestMethodWithGenericParameter(a, b, new ValueA[] { c });
                if (result != (a + 1) + Convert.ToInt32(b) + c.Value)
                {
                    throw new ApplicationException($"Castle.DynamicProxy.TestMethodWithGenericParameter fail, actual:{result}, expected:{(a + 1) + Convert.ToInt32(b) + c.Value}");
                }
            });
            CodeTimerAdvance.TimeByConsole("Castle.DynamicProxy.TestMethodWithGenericParameterAndRefParameter", COUNT, times =>
            {
                var a = 1;
                var b = "2";
                var c = new ValueA {
                    Value = 3
                };
                var result = proxy.TestMethodWithGenericParameterAndRefParameter(a, b, ref c);
                if (result != c.Value)
                {
                    throw new ApplicationException("Castle.DynamicProxy.TestMethodWithGenericParameterAndRefParameter fail");
                }
            });
        }
        public static void TimeByConsole(string title, int times, Action <int> action)
        {
            ConsoleColor foregroundColor = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("{0,16}:\r\n", title);
            Console.ForegroundColor = ConsoleColor.Cyan;

            var codeTimerAdvance = new CodeTimerAdvance
            {
                Times        = times,
                Action       = action,
                ShowProgress = true
            };

            codeTimerAdvance.Execute();

            Console.WriteLine(codeTimerAdvance.ToString());
            Console.ForegroundColor = foregroundColor;
        }