Ejemplo n.º 1
0
        public void Test_EmitManager_Field()
        {
            var manager = new EmitManager();

            var te = manager.CreateInstance <TestEmit>();

            Assert.IsNull(te.Name);

            manager.SetField(te, "Age", 1);
            Assert.AreEqual(1, manager.GetField(te, "Age"));
            Assert.AreEqual(1, manager.GetField(te, typeof(TestEmit).GetField("Age")));

            Exception exception = null;

            try
            {
                DynamicEngine.GetField(te, "Age2");
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                DynamicEngine.SetField(te, "Age3", 8);
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);
        }
Ejemplo n.º 2
0
        public void Test_EmitManager_Field()
        {
            var manager = new EmitManager();

            var te = manager.CreateInstance<TestEmit>();
            Assert.IsNull(te.Name);

            manager.SetField(te, "Age", 1);
            Assert.AreEqual(1, manager.GetField(te, "Age"));
            Assert.AreEqual(1, manager.GetField(te, typeof(TestEmit).GetField("Age")));

            Exception exception = null;
            try
            {
                DynamicEngine.GetField(te, "Age2");
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);

            exception = null;
            try
            {
                DynamicEngine.SetField(te, "Age3", 8);
            }
            catch (MissingMemberException e)
            {
                exception = e;
            }
            Assert.IsNotNull(exception);
        }
Ejemplo n.º 3
0
        private static void CompareReflectAndEmitAccess()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            System.Console.WriteLine("=======  Compare Reflect and Emit Access performance =======");

            var viewModel = new TestViewModel();
            const int count = 80000;

            var reflectManager = new ReflectManager();
            var emitManager = new EmitManager();

            viewModel.TestViewModelEvent += viewModel_TestViewModelEvent;

            // 1. SetProperty
            var sw = new Stopwatch();
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.SetProperty(viewModel, "Age", i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("1. SetProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.SetProperty(viewModel, "Age", i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("1. SetProperty Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 2. GetProperty
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.GetProperty(viewModel, "Age");
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("2. GetProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.GetProperty(viewModel, "Age");
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("2. GetProperty Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 3. Get Index property
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.GetIndexProperty(viewModel, 1);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("3. GetIndexProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.GetIndexProperty(viewModel, 1);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("3. GetIndexProperty Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 4. Set Index property
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.SetIndexProperty(viewModel, i, i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("4. SetIndexProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.SetIndexProperty(viewModel, i, i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("4. SetIndexProperty Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 5. Create Instance
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.CreateInstance<TestViewModel>();
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("5. CreateInstance<T> Reflect time elapsed {0}", sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.CreateInstance<TestViewModel>();
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("5. CreateInstance<T> Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 6. Get method info
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.GetMethodInfo(typeof(TestViewModel), "SetAge", new object[] { 1 });
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("6. GetMethodInfo Reflect time elapsed {0}", sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.GetMethodInfo(typeof(TestViewModel), "SetAge", new object[] { 1 });
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("6. GetMethodInfo Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 7. Get Field
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.GetField(viewModel, "_name");
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("7. GetField Reflect time elapsed {0}", sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.GetField(viewModel, "_name");
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("7. GetField Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 8. Set Field
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.SetField(viewModel, "_age", i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("8. SetField Reflect time elapsed {0}", sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.SetField(viewModel, "_age", i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("8. SetField Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 9. Invoke Method
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.InvokeMethod(viewModel, "SetAge", new object[] { i });
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("9. Invoke method Reflect time elapsed {0}", sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.InvokeMethod(viewModel, "SetAge", new object[] { i });
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("9. Invoke method Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 10. Raise Event
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.RaiseEvent(viewModel, "TestViewModelEvent", null);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("10. Raise event Reflect time elapsed {0}", sw.ElapsedMilliseconds);

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.RaiseEvent(viewModel, "TestViewModelEvent", null);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("10. Raise event Emit time elapsed {0}", sw.ElapsedMilliseconds);
        }
Ejemplo n.º 4
0
        private static void CompareReflectAndEmitAccess()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            System.Console.WriteLine("=======  Compare Reflect and Emit Access performance =======");

            var       viewModel = new TestViewModel();
            const int count     = 80000;

            var reflectManager = new ReflectManager();
            var emitManager    = new EmitManager();

            viewModel.TestViewModelEvent += viewModel_TestViewModelEvent;

            // 1. SetProperty
            var sw = new Stopwatch();

            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.SetProperty(viewModel, "Age", i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("1. SetProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds);


            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.SetProperty(viewModel, "Age", i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("1. SetProperty Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 2. GetProperty
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.GetProperty(viewModel, "Age");
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("2. GetProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds);


            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.GetProperty(viewModel, "Age");
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("2. GetProperty Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 3. Get Index property
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.GetIndexProperty(viewModel, 1);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("3. GetIndexProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds);


            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.GetIndexProperty(viewModel, 1);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("3. GetIndexProperty Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 4. Set Index property
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.SetIndexProperty(viewModel, i, i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("4. SetIndexProperty Reflect time elapsed {0}", sw.ElapsedMilliseconds);


            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.SetIndexProperty(viewModel, i, i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("4. SetIndexProperty Emit time elapsed {0}", sw.ElapsedMilliseconds);


            // 5. Create Instance
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.CreateInstance <TestViewModel>();
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("5. CreateInstance<T> Reflect time elapsed {0}", sw.ElapsedMilliseconds);


            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.CreateInstance <TestViewModel>();
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("5. CreateInstance<T> Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 6. Get method info
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.GetMethodInfo(typeof(TestViewModel), "SetAge", new object[] { 1 });
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("6. GetMethodInfo Reflect time elapsed {0}", sw.ElapsedMilliseconds);


            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.GetMethodInfo(typeof(TestViewModel), "SetAge", new object[] { 1 });
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("6. GetMethodInfo Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 7. Get Field
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.GetField(viewModel, "_name");
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("7. GetField Reflect time elapsed {0}", sw.ElapsedMilliseconds);


            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.GetField(viewModel, "_name");
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("7. GetField Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 8. Set Field
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.SetField(viewModel, "_age", i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("8. SetField Reflect time elapsed {0}", sw.ElapsedMilliseconds);


            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.SetField(viewModel, "_age", i);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("8. SetField Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 9. Invoke Method
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.InvokeMethod(viewModel, "SetAge", new object[] { i });
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("9. Invoke method Reflect time elapsed {0}", sw.ElapsedMilliseconds);


            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.InvokeMethod(viewModel, "SetAge", new object[] { i });
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("9. Invoke method Emit time elapsed {0}", sw.ElapsedMilliseconds);

            // 10. Raise Event
            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                reflectManager.RaiseEvent(viewModel, "TestViewModelEvent", null);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.White;
            System.Console.WriteLine("10. Raise event Reflect time elapsed {0}", sw.ElapsedMilliseconds);


            sw.Reset();
            sw.Start();

            for (int i = 0; i < count; i++)
            {
                emitManager.RaiseEvent(viewModel, "TestViewModelEvent", null);
            }

            sw.Stop();
            Console.ForegroundColor = ConsoleColor.Yellow;
            System.Console.WriteLine("10. Raise event Emit time elapsed {0}", sw.ElapsedMilliseconds);
        }